Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I Use Datastore to Save Leaderstats?[Solved]

Asked by 6 years ago
Edited 6 years ago

So, this has been troubling me a lot lately. I'm still very new to Datastore. So I made a datastore script, but testing it out in Studio, it won't seem to work. The script:

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

Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

Wiki. Read it. http://wiki.roblox.com/index.php?title=Data_store

Ad

Answer this question