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

Why is my Save playerdata script not working?

Asked by 2 years ago

Hello! I am not good at scripting, in fact I started one week ago that is also why I have no idea why this script is not working.

this script is not made by me I just changed the names of the leaderstats ("Money", "Kills"). When I run the game, I get no errors. it is just not doing anything, whitch makes no sense.

It basically should save the players leaderstats and also should load them when you join the game. If you have a better code please send it to me or correct whats wrong with this one:

local ds = game:GetService("DataStoreService"):GetDataStore("StatusStore0")

    ns0 = "leaderstats"

    nc0 = "Money"
    nc1 = "Kills"

    sc0 = 0
    sc1 = 0

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

    local savedLevel = ds:GetAsync(player.UserId)

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

    local coins = Instance.new("NumberValue")
    coins.Name = nc0
    coins.Parent = stats

    local coins1 = Instance.new("NumberValue")
    coins1.Name = nc1
    coins1.Parent = stats

    if savedLevel then
        coins.Value = savedLevel[1]
        coins1.Value = savedLevel[2]
    else
        coins.Value = sc0
        coins1.Value = sc1

        ds:SetAsync(player.UserId,{coins.Value, coins1.Value})
    end
end)

    game.Players.PlayerRemoving:Connect(function(player)
        local stats = player:FindFirstChild(ns0)

        if not stats then return end
        local vals = {stats:FindFirstChild(nc0),stats:FindFirstChild(nc1)}
        if vals and vals[1] and vals[2] then
        ds:SetAsync(player.UserId,{vals[1].Value, vals[2].Value})
    end
end)

1 answer

Log in to vote
0
Answered by 2 years ago

Here is DataStore script I have that works

local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore("leaderstats")

local loaded = {}

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    if player.UserId > 0 and player.Parent then
        local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
        if leaderstatsData ~= "Request rejected" then
            if leaderstatsData then
                for i, Stat in ipairs(leaderstats:GetChildren()) do
                    local value = leaderstatsData[Stat.Name]
                    if value then
                        Stat.Value = value
                    end
                end
            end
            loaded[player] = true
        end
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        if loaded[player] then
            local leaderstatsData = {}
            for i, stat in ipairs(leaderstats:GetChildren()) do 
                leaderstatsData[stat.Name] = stat.Value
            end
            leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
        end
    end
    loaded[player] = nil
end)

This is a little hard to explain so basically When a player joins we wait for their leader stats to be created from the script below, then we make sure that the player Userid is loaded by making sure it's above 0, after we load are leader stats and create a loop for each stat replacing the values as needed. Now for saving them we do the same thing by locating their leader stats, then creating a table and inserting each leaderstats into that table, and then sending that table to the dataStore

And as far as your leader stats you do need to name the folder "leaderstats" or else it will not work

Here is an example of the leader starts script I have:

local StatsTable = {
    "Cash",
    "Rebirths",
    "Kills",
    "KOs"
}


game.Players.PlayerAdded:Connect(function(p)
    local leaderstats = Instance.new("Folder", p)
    leaderstats.Name = "leaderstats"


    for i = 1, #StatsTable do
        local stat = Instance.new("IntValue", leaderstats)
        stat.Name = StatsTable[i]
        stat.Value = 0 
    end

end)

To explain this script P is Player and all I'm doing is when the player is added I create a folder and make the parent the player, Then I name it accordingly.

In the For loop, it just goes through each Stat I have in the table above and creates a new IntValue and names it, and sets it to value accordingly.

Hopefully, this helps, or at least you can use it to try to see if you can make some sense out of it.

0
So, I just put it in the ServerScriptService and it is still not working. I published the game then I joined and played a bit so my stats are changing. Then I logged off and then logged on again and it saved no stats. What am I making wrong? Should I make the game public first? Because obviously this is my fault. Lozock_HD 2 — 2y
0
You do need to make sure you allow the api for your game Via GameSettings < Security < Enable Studio Access to API Services. Along with this you cant chang the value in studio unless its through a script but if you publish the game and then opent the devloper consol and type in "game.Players.*YourName*.leaderstats.Cash.Value = 100" and rejoin it should save. Lastly they are 2 diffrent scripts dethshoot3987 15 — 2y
0
I enabled Studio Access to API Services and I also changed the stats by playing it through the website and then joined the game. I also put those into two diffrent scripts. And it still is not working. It is not making any sense in my eyes that this is not working. I can show you the game if you want. Lozock_HD 2 — 2y
0
Okay I think I have found the problem now. As I thought the problem was not becouse you it was becouse I have the thing that updates the leaderstats in a localscript. So others can’t see what stats I have and your script just saved 0. Lozock_HD 2 — 2y
Ad

Answer this question