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

Data store for a leaderstat does not work? [closed]

Asked by 6 years ago

This question already has an answer here:

Data store for a leaderstat does not work?

Hello, I am working on a game that uses a data store script to save the amount of currency you have but for some reason it does not work.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("ToadsSaver")

game.Players.PlayerAdded:connect(function(player)
    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"
    local Toads = Instance.new("IntValue",leader)
    Toads.Name = "Toads"
    Toads.Value = ds:GetAsync(player.UserId) or 0
    ds:SetAsync(player.UserId, Toads.Value)
    Toads.Changed:connect(function()
        ds:SetAsync(player.UserId, Toads.Value
    end)
end)


game.Players.PlayerRemoving:connect(function(player)
    ds.SetAsync(player.UserId, player.leaderstats.Toads.Value)
end)

Marked as Duplicate by Troidit, cabbler, Leamir, and Gey4Jesus69

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This code will work!

--[[Savin'
        Dem
            Stats   
--]]
game.Players.PlayerRemoving:connect(function(player)
    local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
    datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
    print("saved data number "..i)

end
print("Stats successfully saved")   
end)


--[[
    Loadin'
        Dem
            Stats
--]]
game.Players.PlayerAdded:connect(function(player)
        local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    player:WaitForChild("leaderstats")
    wait(1)
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do            
    stats[i].Value = datastore:GetAsync(stats[i].Name)
    print("stat numba "..i.." has been found")
        end
end)
Ad