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

I can't save data in my game. I have a DataStore script. Can anybody help fix this?

Asked by 4 years ago

So this is a script for DataStore and leaderstats. I do have studio access to API services on.

local serverStorage = game:GetService("ServerStorage")
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave")
game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local strength = Instance.new("NumberValue")
    strength.Name = "Strength"
    strength.Parent = leaderstats

    local rebirths = Instance.new("IntValue")
    rebirths.Name = "Rebirths"
    rebirths.Parent = leaderstats

    local dataFolder = Instance.new("Folder")
    dataFolder.Name = player.Name
    dataFolder.Parent = serverStorage.RemoteData

    local debounce = Instance.new("BoolValue")
    debounce.Name = "Debounce"
    debounce.Parent = dataFolder

    local strengthData, rebirthsData

    local success, errormessage = pcall(function()
        strengthData = dataStore:GetAsync("strength-"..player.UserId)
        rebirthsData = dataStore:GetAsync("rebirths-"..player.UserId)
    end)
    if success then
        if strengthData then
            strength.Value = strengthData
            rebirths.Value = rebirthsData
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        dataStore:SetAsync("strength-"..player.UserId.player.leaderstats.Strength.Value)
        dataStore:SetAsync("rebirths-"..player.UserId.player.leaderstats.Rebirths.Value)
    end)
end)

My game will NOT save data for some reason... If you can help me, please reply or put an answer.

2 answers

Log in to vote
0
Answered by 4 years ago

The reason why it is not saving is that you conjoined player.UserId with the "Strength" and "Rebirths" values on lines 42 and 43. All you have to do is add a comma instead of a period in between both parameters. I have the solution below if you want to see the correct outcome.

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        dataStore:SetAsync("strength-"..player.UserId, player.leaderstats.Strength.Value)
        dataStore:SetAsync("rebirths-"..player.UserId, player.leaderstats.Rebirths.Value)
    end)
end)
0
Thanks! Not_TheOnlyTree 11 — 4y
Ad
Log in to vote
0
Answered by
Soban06 410 Moderation Voter
4 years ago

Do something like this inside the player removing part:

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        dataStore:SetAsync(player.UserId.."-strength", player.leaderstats.Strength.Value)
        dataStore:SetAsync(player.UserId.."-rebirths", player.leaderstats.Rebirths.Value)
    end)
end)

Answer this question