#Data storage
1 messages · Page 1 of 1 (latest)
local DataStoreService = game:GetService("DataStoreService")
local coinsdata = DataStoreService:GetDataStore("coindata")
local runservice = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
local coins = leaderstats:WaitForChild("Coins")
local success , savedcoins = pcall(function()
return coinsdata:GetAsync(plr.UserId)
end)
if success and savedcoins then
coins.Value = savedcoins
else
coins.Value = 0
print("New Player !")
end
local time_wait = 0
runservice.Heartbeat:Connect(function(deltatime)
if plr.Parent ~= nil then
time_wait += deltatime
if time_wait >= 60 then
time_wait = 0
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
print("Saved !")
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
local coins = leaderstats:FindFirstChild("Coins")
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
end)
retries if the get or set fails and game:bindtoclose, maybe session locking, otherwise fairly traditional leaderstats/datastore script
oh wsp @wild veldt
uhmm i dont understand
so u mean if player left the game and cant save ?
wat if a player joins and datastore servers are down or overloaded like grow a garden hits 10mil ccu again? you have no retry load data if it fails
how ?
google is your friend 
i go to it he just add on local success , saveddata ....
if not success then warn("didt save ")
i am new at run service what u mean ?
each player have its own fps
also yes if player leave then heartbeat connection is still there, you never disconnect it. memory leak
it doesn't do anything but it's still going to take up 0.0001 of a frame, every frame.
when player leave heartbeat stop bec i add if plr.parent ~= nil
Never disconects
Still exists
ok i will add else disconnect() ?
that's what you would use for infinite while loop paired with a task.wait() but you connect to heartbeat instead
Sure
ok 1 sec
local DataStoreService = game:GetService("DataStoreService")
local coinsdata = DataStoreService:GetDataStore("coindata")
local runservice = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
local coins = leaderstats:WaitForChild("Coins")
local success , savedcoins = pcall(function()
return coinsdata:GetAsync(plr.UserId)
end)
if success and savedcoins then
coins.Value = savedcoins
else
coins.Value = 0
print("New Player !")
end
local time_wait = 0
local connect = runservice.Heartbeat:Connect(function(deltatime)
if plr.Parent ~= nil then
time_wait += deltatime
if time_wait >= 60 then
time_wait = 0
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
print("Saved !")
end
else
connect:Disconnect()
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
local coins = leaderstats:FindFirstChild("Coins")
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
end)
```like this ?
there is problem here connect:Disconnect()
problem on connect why ?
here is error
ServerScriptService.Datastore:36: attempt to index nil with 'Disconnect'
local connect
connect = ...```
if connect then connect:Disconnect() connect=nil end
etc
if connect then
connect still problem
local connect = runservice.Heartbeat:Connect(function(deltatime)
if plr.Parent ~= nil then
time_wait += deltatime
if time_wait >= 60 then
time_wait = 0
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
print("Saved !")
end
if plr.Parent == nil then
connect:Disconnect()
end
end
end)
here still connect problem idk why
there is orange line under connect in connect:Disconnect()
inline your heartbeat event
to connect variable
wait hold on
do what pyrofire said
i did there is problem
error *
i will just add else print("plr left ") to see if heartbeat stop or no
the issue is the connect variable
local connect
connect = heartbeat
must be 2 lines
its same thing as local connect = runservice....
nope
the function doesnt recognise the variable
tho i am pretty sure it should work, eh, just do both
yes it worked but what is the diffrance
nae
wtf is this code i cant understand line lol
avoids recursive variable
not part of your code
works just fine for functions but apparently it's an issue there
yeah but its very hard man
local DataStoreService = game:GetService("DataStoreService")
local coinsdata = DataStoreService:GetDataStore("coindata")
local runservice = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
local coins = leaderstats:WaitForChild("Coins")
local success , savedcoins = pcall(function()
return coinsdata:GetAsync(plr.UserId)
end)
if success and savedcoins then
coins.Value = savedcoins
else
coins.Value = 0
print("New Player !")
end
local time_wait = 0
local connect
connect = runservice.Heartbeat:Connect(function(deltatime)
if plr.Parent ~= nil then
time_wait += deltatime
if time_wait >= 60 then
time_wait = 0
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
print("Saved !")
end
else
connect:Disconnect()
print("stop")
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local leaderstats = plr:FindFirstChild("leaderstats")
local coins = leaderstats:FindFirstChild("Coins")
pcall(function()
coinsdata:SetAsync(plr.UserId , coins.Value)
end)
end)
here is final code all thing will work correctly right ?
idk does it