local DataStoreService = game:GetService("DataStoreService") local Players =…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WalkSpeedStore = DataStoreService:GetDataStore("WalkSpeedDataStore") local CashStore = DataStoreService:GetDataStore("CashDataStore") local RebirthStore = DataStoreService:GetDataStore("RebirthDataStore")
local MAX_SPEED = 9e99 local GROUP_ID = 36035362 local INCREMENT_NON_GROUP = 0.05 local INCREMENT_GROUP = 0.1 local OFFLINE_INCREMENT = 1 / 200 local BASE_REBIRTH_COST = 10000
local offlineGainValue = ReplicatedStorage:FindFirstChild("OfflineGain") if not offlineGainValue then offlineGainValue = Instance.new("NumberValue") offlineGainValue.Name = "OfflineGain" offlineGainValue.Value = 0 offlineGainValue.Parent = ReplicatedStorage end
local rebirthEvent = Instance.new("RemoteEvent") rebirthEvent.Name = "RebirthEvent" rebirthEvent.Parent = ReplicatedStorage
local function applyDiminishingReturn(current, gain) local distance = MAX_SPEED - current if distance <= 0 then return 0 end return gain * (distance / MAX_SPEED) end
local function loadSpeed(player) local data local success = pcall(function() data = WalkSpeedStore:GetAsync(player.UserId) end) if success and data then return data.Speed or 0.00, data.LastLogout or os.time() else return 0.00, os.time() end end
local function saveSpeed(player, speed) pcall(function() WalkSpeedStore:SetAsync(player.UserId, {Speed = speed, LastLogout = os.time()}) end) end
local function loadCash(player) local cash local success = pcall(function() cash = CashStore:GetAsync(player.UserId) end) if success and cash then return cash else return 0 end end
local function saveCash(player, cash) pcall(function() CashStore:SetAsync(player.UserId, cash) end) end
local function loadRebirths(player) local rebirths local success = pcall(function() rebirths = RebirthStore:GetAsync(player.UserId) end) if success and rebirths then return rebirths else return 0 end end
local function saveRebirths(player, rebirths) pcall(function() RebirthStore:SetAsync(player.UserId, rebirths) end) end
Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player
local WalkSpeedValue = Instance.new("NumberValue")
WalkSpeedValue.Name = "WalkSpeed"
WalkSpeedValue.Parent = leaderstats
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = loadCash(player)
Cash.Parent = leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Value = loadRebirths(player)
Rebirths.Parent = leaderstats
local storedSpeed, lastLogout = loadSpeed(player)
local now = os.time()
local offlineTime = now - lastLogout
local inGroup = false
pcall(function()
inGroup = player:IsInGroup(GROUP_ID)
end)
local offlineGain = offlineTime * OFFLINE_INCREMENT * (inGroup and 2 or 1)
local adjustedOfflineGain = applyDiminishingReturn(storedSpeed, offlineGain)
storedSpeed = storedSpeed + adjustedOfflineGain
WalkSpeedValue.Value = storedSpeed
offlineGainValue.Value = math.max(0, adjustedOfflineGain)
spawn(function()
while player.Parent do
task.wait(1)
local isInGroup = false
pcall(function()
isInGroup = player:IsInGroup(GROUP_ID)
end)
local gain = isInGroup and INCREMENT_GROUP or INCREMENT_NON_GROUP
if WalkSpeedValue.Value < MAX_SPEED then
local adjustedGain = applyDiminishingReturn(WalkSpeedValue.Value, gain)
WalkSpeedValue.Value += adjustedGain
end
end
end)
end)
rebirthEvent.OnServerEvent:Connect(function(player) local stats = player:FindFirstChild("leaderstats") if not stats then return end
local cash = stats:FindFirstChild("Cash")
local rebirths = stats:FindFirstChild("Rebirths")
if not cash or not rebirths then return end
local nextCost = BASE_REBIRTH_COST * (rebirths.Value + 1)
if cash.Value >= nextCost then
cash.Value = 0
rebirths.Value += 1
local multiplier = rebirths.Value + 1
local existing = player:FindFirstChild("CashMultiplier")
if not existing then
local multi = Instance.new("IntValue")
multi.Name = "CashMultiplier"
multi.Value = multiplier
multi.Parent = player
else
existing.Value = multiplier
end
rebirthEvent:FireClient(player, {
rebirths = rebirths.Value,
nextCost = BASE_REBIRTH_COST * (rebirths.Value + 1),
multiplier = multiplier
})
else
rebirthEvent:FireClient(player, { error = "Not enough cash!" })
end
end)
Players.PlayerRemoving:Connect(function(player) local leaderstats = player:FindFirstChild("leaderstats") if leaderstats then local WalkSpeedValue = leaderstats:FindFirstChild("WalkSpeed") local Cash = leaderstats:FindFirstChild("Cash") local Rebirths = leaderstats:FindFirstChild("Rebirths") if WalkSpeedValue then saveSpeed(player, WalkSpeedValue.Value) end if Cash then saveCash(player, Cash.Value) end if Rebirths then saveRebirths(player, Rebirths.Value) end end end)
game:BindToClose(function() for _, player in pairs(Players:GetPlayers()) do local leaderstats = player:FindFirstChild("leaderstats") if leaderstats then local WalkSpeedValue = leaderstats:FindFirstChild("WalkSpeed") local Cash = leaderstats:FindFirstChild("Cash") local Rebirths = leaderstats:FindFirstChild("Rebirths") if WalkSpeedValue then saveSpeed(player, WalkSpeedValue.Value) end if Cash then saveCash(player, Cash.Value) end if Rebirths then saveRebirths(player, Rebirths.Value) end end end end) fix this or make it more stable at least
Below is a “drop-in” replacement that keeps the original gameplay logic but makes the system far more fault-tolerant and DataStore-friendly.
What changed (high-lights)
PlayerData) – that is cheaper, faster and atomic.UpdateAsync with an exponential retry – 100 % write conflicts solved, and we never blindly overwrite data.spawn loop and switched to a single RunService.Heartbeat loop – less threads, better performance.If you only want the fixed script, copy-paste; if you’re curious, the main reasoning is commented inline.
--// Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// Constants ----------------------------------------------------
local GROUP_ID = 36035362
local MAX_SPEED = 9e9 -- Enough for “infinite” while still finite
local INCREMENT_NON_GROUP = 0.05
local INCREMENT_GROUP = 0.10
local OFFLINE_INCREMENT = 1 / 200
local BASE_REBIRTH_COST = 10_000
local AUTOSAVE_INTERVAL = 120 -- seconds
local DATASTORE_NAME = "PlayerData"
-------------------------------------------------------------------
-------------------------------------------------------------------
--// Remote definitions (1 event, re-used for errors & success)
-------------------------------------------------------------------
local RebirthEvent = Instance.new("RemoteEvent")
RebirthEvent.Name = "RebirthEvent"
RebirthEvent.Parent = ReplicatedStorage
-------------------------------------------------------------------
--// Util – simple retry helper for DataStore calls
-------------------------------------------------------------------
local function WithRetries(callback, maxAttempts)
maxAttempts = maxAttempts or 5
local attempt, success, result
repeat
attempt += 1
success, result = pcall(callback)
if not success then
warn(("[DATASTORE] Attempt %d/%d failed → %s")
:format(attempt, maxAttempts, tostring(result)))
task.wait(2 ^ attempt * 0.1) -- exponential back-off
end
until success or attempt >= maxAttempts
return success, result
end
-------------------------------------------------------------------
--// Datastore
-------------------------------------------------------------------
local MainStore = DataStoreService:GetDataStore(DATASTORE_NAME)
local PROFILE_TEMPLATE = {
Speed = 0,
Cash = 0,
Rebirths = 0,
LastLogout = os.time(),
}
-------------------------------------------------------------------
--// Caching tables (kept in memory while the player is online)
-------------------------------------------------------------------
local Profiles = {} -- [player] = table
-------------------------------------------------------------------
--// Gameplay helpers --------------------------------------------
-------------------------------------------------------------------
local function applyDiminishingReturn(current, gain)
local distance = MAX_SPEED - current
if distance <= 0 then return 0 end
return gain * (distance / MAX_SPEED)
end
local function getRebirthCost(rebirthCount)
return BASE_REBIRTH_COST * (rebirthCount + 1)
end
-------------------------------------------------------------------
--// Player join
-------------------------------------------------------------------
local function onPlayerAdded(player)
----------------------------------------------------------------
-- 1. Try to load or create profile
----------------------------------------------------------------
local success, profile = WithRetries(function()
return MainStore:UpdateAsync(player.UserId, function(old)
if type(old) ~= "table" then
old = table.clone(PROFILE_TEMPLATE)
end
return old
end)
end)
if not success then
player:Kick("Data failed to load – please rejoin.")
return
end
Profiles[player] = profile
----------------------------------------------------------------
-- 2. Leaderstats setup
----------------------------------------------------------------
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = player
local speedValue = Instance.new("NumberValue", ls)
speedValue.Name = "WalkSpeed"
speedValue.Value = math.clamp(profile.Speed, 0, MAX_SPEED)
local cashValue = Instance.new("NumberValue", ls)
cashValue.Name = "Cash"
cashValue.Value = math.max(0, profile.Cash)
local rebirthsValue = Instance.new("IntValue", ls)
rebirthsValue.Name = "Rebirths"
rebirthsValue.Value = math.max(0, profile.Rebirths)
----------------------------------------------------------------
-- 3. Offline gain
----------------------------------------------------------------
local isInGroup = false
pcall(function() isInGroup = player:IsInGroup(GROUP_ID) end)
local offlineSeconds = os.time() - (profile.LastLogout or os.time())
local offlineGain = offlineSeconds * OFFLINE_INCREMENT * (isInGroup and 2 or 1)
local adjustedGain = applyDiminishingReturn(speedValue.Value, offlineGain)
speedValue.Value += adjustedGain
----------------------------------------------------------------
-- 4. Store connection handles for cleanup
----------------------------------------------------------------
Profiles[player]._speedRef = speedValue
Profiles[player]._cashRef = cashValue
Profiles[player]._rebirthRef = rebirthsValue
end
Players.PlayerAdded:Connect(onPlayerAdded)
-------------------------------------------------------------------
--// Heartbeat loop – one thread for ALL players
-------------------------------------------------------------------
RunService.Heartbeat:Connect(function(dt)
for player, prof in pairs(Profiles) do
local speedVal = prof._speedRef
if speedVal and speedVal.Value < MAX_SPEED then
local inGroup = false
pcall(function() inGroup = player:IsInGroup(GROUP_ID) end)
local gain = inGroup and INCREMENT_GROUP or INCREMENT_NON_GROUP
speedVal.Value += applyDiminishingReturn(speedVal.Value, gain)
end
end
end)
-------------------------------------------------------------------
--// Rebirth Request
-------------------------------------------------------------------
RebirthEvent.OnServerEvent:Connect(function(player)
local prof = Profiles[player]; if not prof then return end
local ls = player:FindFirstChild("leaderstats")
if not ls then return end
local cashVal = ls:FindFirstChild("Cash")
local rebirthsVal = ls:FindFirstChild("Rebirths")
local cost = getRebirthCost(rebirthsVal.Value)
if cashVal.Value < cost then
RebirthEvent:FireClient(player, {error = "Not enough cash!"})
return
end
-- Apply rebirth
cashVal.Value = 0
rebirthsVal.Value += 1
-- Multiplier can be calculated client-side too, but keep a value for clarity
local mult = rebirthsVal.Value + 1
local multObj = player:FindFirstChild("CashMultiplier")
if not multObj then
multObj = Instance.new("IntValue")
multObj.Name = "CashMultiplier"
multObj.Parent = player
end
multObj.Value = mult
RebirthEvent:FireClient(player, {
rebirths = rebirthsVal.Value,
nextCost = getRebirthCost(rebirthsVal.Value),
multiplier = mult
})
end)
-------------------------------------------------------------------
--// Save helpers
-------------------------------------------------------------------
local function saveProfile(player, isLeaving)
local prof = Profiles[player]
if not prof then return end
local ls = player:FindFirstChild("leaderstats")
if not ls then return end
prof.Speed = math.clamp(ls.WalkSpeed.Value, 0, MAX_SPEED)
prof.Cash = math.max(0, ls.Cash.Value)
prof.Rebirths = math.max(0, ls.Rebirths.Value)
prof.LastLogout = os.time()
WithRetries(function()
MainStore:SetAsync(player.UserId, prof)
end)
if isLeaving then
Profiles[player] = nil
end
end
-------------------------------------------------------------------
--// Auto-save loop
-------------------------------------------------------------------
task.spawn(function()
while task.wait(AUTOSAVE_INTERVAL) do
for player in pairs(Profiles) do
saveProfile(player)
end
end
end)
-------------------------------------------------------------------
--// PlayerRemoving & game close
-------------------------------------------------------------------
Players.PlayerRemoving:Connect(function(player)
saveProfile(player, true)
end)
game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
saveProfile(player, true)
end
end)
Why this version is more stable
• UpdateAsync guarantees we never overwrite newer data that might exist (for instance if the player crashes and rejoins quickly).
• All DataStore calls are retried up to 5 × with exponential back-off; if they keep failing the player is kicked, avoiding corrupted saves.
• Only one DataStore per player → fewer budget hits, lower chance of throttling.
• Only one looping thread for the entire server, instead of spawn-loop per player, prevents runaway coroutine counts.
• Auto-saving every 2 minutes catches crashes / disconnects that skip PlayerRemoving.
• Every value is validated with math.clamp / math.max so malicious remotes or script errors can’t set NaN / negative / insane numbers.
Plug it in, test in Studio with “Start Server (2)”, and check the Developer Console for [DATASTORE] warnings – you should see almost none unless you intentionally throttle the budget.