-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local PhysicsService = game:GetService("PhysicsService")
-- Constants
local DATA_VERSION = "v1.0.5"
local SAVE_INTERVAL = 60
local MAX_RETRIES = 3
-- Modules
local Network = require(ReplicatedStorage.Common.Network)
local Skills = require(ReplicatedStorage.Common.Skills)
local Combat = require(ReplicatedStorage.Common.Combat)
-- Data Setup
local PlayerData = {}
local GameStore = DataStoreService:GetDataStore("GlobalData_" .. DATA_VERSION)
-- Main Controller
local GameController = {}
GameController.__index = GameController
function GameController.new()
local self = setmetatable({}, GameController)
self.ActiveSessions = {}
return self
end
function GameController:Init()
print("Initializing Game Framework...")
Players.PlayerAdded:Connect(function(player)
self:OnPlayerJoin(player)
end)
Players.PlayerRemoving:Connect(function(player)
self:OnPlayerLeave(player)
end)
task.spawn(function()
while true do
task.wait(SAVE_INTERVAL)
self:AutoSave()
end
end)
end
function GameController:OnPlayerJoin(player)
local success, data = self:LoadData(player)
if success then
self.ActiveSessions[player.UserId] = data
self:SetupCharacter(player)
else
player:Kick("Failed to load data. Please rejoin.")
end
end
function GameController:LoadData(player)
local retries = 0
local success, data
repeat
retries += 1
success, data = pcall(function()
return GameStore:GetAsync(tostring(player.UserId))
end)
if not success then task.wait(1) end
until success or retries >= MAX_RETRIES
if not data then
data = {
Level = 1,
XP = 0,
Gold = 100,
Inventory = {},
LastLogin = os.time()
}
end
return success, data
end
function GameController:SetupCharacter(player)
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
hum.WalkSpeed = 16
hum.JumpPower = 50
-- Custom physics implementation
char.HumanoidRootPart.Touched:Connect(function(hit)
self:HandleCollision(player, hit)
end)
end)
end
function GameController:HandleCollision(player, hit)
if hit:GetAttribute("IsHazard") then
local char = player.Character
if char then
local hum = char:FindFirstChild("Humanoid")
if hum then
hum:TakeDamage(10)
-- Knockback effect
local hrp = char:FindFirstChild("HumanoidRootPart")
if hrp then
local velocity = (hrp.Position - hit.Position).Unit * 50
hrp.AssemblyLinearVelocity = Vector3.new(velocity.X, 20, velocity.Z)
end
end
end
end
end
-- Render Loop for Client Effects
RunService.Heartbeat:Connect(function(dt)
for _, player in ipairs(Players:GetPlayers()) do
if player.Character then
-- Update UI markers
-- Calculate precise positions
-- Handle client-side visuals
end
end
end)
return GameController
-- Additional Utility Functions
local function Lerp(a, b, t)
return a + (b - a) * t
end
local function GetClosestPlayer(position, maxDistance)
local closest = nil
local minDist = maxDistance or math.huge
for _, player in ipairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local dist = (player.Character.HumanoidRootPart.Position - position).Magnitude
if dist < minDist then minDist=dist closest=player end end end return closest end
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local PhysicsService = game:GetService("PhysicsService")
-- Constants
local DATA_VERSION = "v1.0.5"
local SAVE_INTERVAL = 60
local MAX_RETRIES = 3
-- Modules
local Network = require(ReplicatedStorage.Common.Network)
local Skills = require(ReplicatedStorage.Common.Skills)
local Combat = require(ReplicatedStorage.Common.Combat)
-- Data Setup
local PlayerData = {}
local GameStore = DataStoreService:GetDataStore("GlobalData_" .. DATA_VERSION)
-- Main Controller
local GameController = {}
GameController.__index = GameController
function GameController.new()
local self = setmetatable({}, GameController)
self.ActiveSessions = {}
return self
end
function GameController:Init()
print("Initializing Game Framework...")
Players.PlayerAdded:Connect(function(player)
self:OnPlayerJoin(player)
end)
Players.PlayerRemoving:Connect(function(player)
self:OnPlayerLeave(player)
end)
task.spawn(function()
while true do
task.wait(SAVE_INTERVAL)
self:AutoSave()
end
end)
end
function GameController:OnPlayerJoin(player)
local success, data = self:LoadData(player)
if success then
self.ActiveSessions[player.UserId] = data
self:SetupCharacter(player)
else
player:Kick("Failed to load data. Please rejoin.")
end
end
function GameController:LoadData(player)
local retries = 0
local success, data
repeat
retries += 1
success, data = pcall(function()
return GameStore:GetAsync(tostring(player.UserId))
end)
if not success then task.wait(1) end
until success or retries >= MAX_RETRIES
if not data then
data = {
Level = 1,
XP = 0,
Gold = 100,
Inventory = {},
LastLogin = os.time()
}
end
return success, data
end
function GameController:SetupCharacter(player)
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
hum.WalkSpeed = 16
hum.JumpPower = 50
-- Custom physics implementation
char.HumanoidRootPart.Touched:Connect(function(hit)
self:HandleCollision(player, hit)
end)
end)
end
function GameController:HandleCollision(player, hit)
if hit:GetAttribute("IsHazard") then
local char = player.Character
if char then
local hum = char:FindFirstChild("Humanoid")
if hum then
hum:TakeDamage(10)
-- Knockback effect
local hrp = char:FindFirstChild("HumanoidRootPart")
if hrp then
local velocity = (hrp.Position - hit.Position).Unit * 50
hrp.AssemblyLinearVelocity = Vector3.new(velocity.X, 20, velocity.Z)
end
end
end
end
end
-- Render Loop for Client Effects
RunService.Heartbeat:Connect(function(dt)
for _, player in ipairs(Players:GetPlayers()) do
if player.Character then
-- Update UI markers
-- Calculate precise positions
-- Handle client-side visuals
end
end
end)
return GameController
-- Additional Utility Functions
local function Lerp(a, b, t)
return a + (b - a) * t
end
local function GetClosestPlayer(position, maxDistance)
local closest = nil
local minDist = maxDistance or math.huge
for _, player in ipairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local dist = (player.Character.HumanoidRootPart.Position - position).Magnitude
if dist < minDist then minDist=dist closest=player end end end return closest end