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

Script cannot read leaderstats coins value?

Asked by 5 years ago

Hey! So I'm trying to make a very basic coin save and load. It is meant to load the player coins when it enters, and save them when it leaves. However it always saves the coins value as 0, and if I try to just read the coins value from a normal script, it reads it as 0 no matter what value is stored. However if I try to read the value from a LocalScript it does read the value.

What is wrong?

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

game.Players.PlayerAdded:Connect(function(player)
    local leader = Instance.new("Folder", player)
    leader.Name = "leaderstats"
    local coins = Instance.new("IntValue", leader)
    coins.Name = "Coins"
    local success, coinsData = pcall(
        function()
            coins.Value = ds:GetAsync(player.UserId, player.leaderstats.Coins.Value)        
        end
    )
    if success then
        print ("success")
        else print("Failed")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    --print("player left")
    local success, err = pcall(
        function()
            --print("func")
            print(player.leaderstats.Coins.Value)
            ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
        end
    )
    if success then
        print("success")
        else print("Failed")
    end
end)
0
You will need RemoteEvent if the client needs to do something the server cannot, such as user input, for the changes to replicate to thes server. User#24403 69 — 5y
0
Use RemoteEvents. LoganboyInCO 150 — 5y

1 answer

Log in to vote
0
Answered by
Kikitob 105
5 years ago

You are changing the value on the client. You will have to use RemoteEvents or RemoteFunctions to change the value on the server so that it replicates.

Ad

Answer this question