#Leaderstats

1 messages · Page 1 of 1 (latest)

supple ridge
#

Why the leaderstats don't work?

Code :

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local SaveKeys = {"Coins", "Diamonds"}

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    for _, key in ipairs(SaveKeys) do
        local value = Instance.new("IntValue")
        value.Name = key
        value.Parent = leaderstats
    end
    
    local success, data = pcall(function()
        return PlayerData:GetAsync(player.UserId .."_Data")
    end)
    
    if success and data then
        for _, key in ipairs(SaveKeys) do
            if data[key] then
                player.leaderstats[key].Value = data[key]
            else
                player.leaderstats[key].Value = 0
            end
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local SaveData = {}
    
    for _, key in ipairs(SaveKeys) do
        SaveData[key] = player.leaderstats[key].Value
    end
    
    pcall(function()
        return PlayerData:SetAsync(player.UserId .."_Data", SaveData)
    end)
end)
drifting phoenix
#

Any erros? I assume you didn't enable HTTP and studio settings things, that allow you to use Datastore in Studio

supple ridge
drifting phoenix
#

I don't belive so. Maybe it does.

supple ridge
#

i'm not in Kazahstan or Russia

supple ridge
#

Bro

#

there was something wrong

#

see this code:

    local leaderstats = player:WaitForChild("leaderstats")
    local coins = leaderstats:WaitForChild("Coins").Value
    print(coins)

The coins value print 0, but shows in the leaderstats is 17, but how?

supple ridge
#

I couldn't understand, what is this, i don't know!

devout harbor
#

I think I know, it's probably because you got the coins on the client side and not server side

#

That's probably it, so you should use a remote event or something like that to switch from client to server in your click script

#

Because your leaderstats is a server script and when you got the coins you mustve got them from a local script right?

supple ridge
#

Finally fixed! Thank you

devout harbor
supple ridge