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

how to fix a datastore for leaderstats?

Asked by 3 years ago

Hello, I am attempting to save data in my game but the code is not working. I'm not sure of the problem and I'm pretty sure many others are having the same problem. My code is below. ~~~~~~~~~~~~~~~~~ local DataStoreService = game:GetService("DataStoreService") local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore") game.Players.PlayerAdded:Connect(function(player) local Folder = Instance.new("Folder",player) Folder.Name = "leaderstats" Folder.Parent = player local Samples = Instance.new("IntValue",Folder) Samples.Name = "Samples"

local coins = Instance.new("IntValue",Folder)
coins.Name = "Coins"
coins.Parent = Folder

local playerUserId = "Player"..player.UserId

--Load Data
local data
local success,errormessage = pcall(function()
    data = CoinsDataStore:GetAsync(playerUserId)
end)


if success then
    if data then
        coins.Value = data.coins
        Samples.Value = data.Samples
    end
    -- Set data == current values
end

end)

game.Players.PlayerRemoving:Connect(function(player) local playerUserId = "Player_"..player.UserId local data = { coins = player.leaderstats.Coins.Value, samples = player.leaderstats.Samples.Value } local success, errormessage = pcall(function() CoinsDataStore:SetAsync(playerUserId, data) end) if success then print("Data successfully saved!") else print("Error saving data! If you see this do not leave the game or your save data will be deleted! Stay here while the problem is resolved!") warn(errormessage) end end) ~~~~~~~~~~~~~~~~~

--All of that is the code idk why it is not displaying it all in the code part.

1
you have to make sure the "~" things are either completely below or completely above the code in order to fit it into the format Omq_ItzJasmin 666 — 3y

2 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago
local DataStoreService = game:GetService("DataStoreService") 
local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore") 

game.Players.PlayerAdded:Connect(function(player) 

    local Folder = Instance.new("Folder",player) 
    Folder.Name = "leaderstats" -- dont need Folder.Parent = player. you already set its parent

    local Samples = Instance.new("IntValue",Folder) 
    Samples.Name = "Samples"

    local coins = Instance.new("IntValue",Folder)
    coins.Name = "Coins"

    local UserId = tostring(player.UserId) -- made it just the userid cos i dont like player with it

    local data
    local success,errormessage = pcall(function()
        data = CoinsDataStore:GetAsync(UserId)
    end)


    if success then
        if data then
            coins.Value = data.coins
            Samples.Value = data.Samples
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local UserId = tostring(player.UserId)
    local data = {
        coins = player.leaderstats.Coins.Value
        Samples = player.leaderstats.Samples.Value -- s needs to be capitalized cause when you load data, you said data.Samples not data.samples
    }
    local success, errormessage = pcall(function()
        CoinsDataStore:SetAsync(UserId, data)
    end)
-- and when you warn 'stay here while problem is solved' they cant. this runs when the player already clicked the leave button.
end)
Ad
Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

In your code on line 13 (or where you say "local playerUserId = "Player"..player.UserId), you save the value as "Player" then whatever userId they have (ex. Player1986328, Player89278). But then, on line 31 in the PlayerRemoved thread, you save "playerUserId" as "local playerUserId = "Player_"..player.UserId", meaning that it will look for "Player_1986328" or "Player_89278". Line 13 and line 31 both save and collect data for different things. Try changing line 13 to:

local playerUserId = "Player_"..player.UserId

so that it matches up with what you are saving on the next thread..

Another problem I noticed was on line 25, when you said "Samples.Value = data.samples" but then later on, you identify the data as "samples = player.leaderstats.Samples.Value". You should capitalize that line:

Samples = player.leaderstats.Samples.Value

hope this helps

1
Thank you both, it now works. verysweatyperson 10 — 3y

Answer this question