Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you save a table using datastores? [Tycoon]

This is my script and I have all the values in leaderstats and :GetChildren() doenst work here so I was wondering how to save a table in a tycoon, also without making all those values?

local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name")
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

-- Loading dem stats
game.Players.PlayerAdded:connect(function(newplayer)
newplayer:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats2 = newplayer:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats2 do
stats2[i].Value = datastore:GetAsync(stats2[i].Name)
end
end)

1 answer

✓ Accepted answer

Well, I've checked your code. Gotta say, that was a weird mistake you've done. When player joins, there is no problem about waiting for dataready, but when player is leaving, any millisecond is against you. And you made it wait for two more seconds in PlayerRemoving function. I guess you copied it from PlayerAdded function so that's a normal mistake if you do not check the code carefully. Just delete player:WaitForDataReady() and wait(2) at line 4 and 5 and it should run fine after that.

1 comment

1

Accept this answer

This discussion is preserved as it was. New answers, comments, and votes are closed.