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

Why does this not save player data?

Asked by
Vxpper 101
4 years ago
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("sAvePLR_Dataa")

game.Players.PlayerAdded:connect(function(plr)  
    local coins = Instance.new("IntValue", plr)
    coins.Name = "Coins"

    local rubys = Instance.new("IntValue", plr)
    rubys.Name = "Rubys"    

    local function getData()
        local getSave = ds1:GetAsync(plr.UserId)

        if getSave then
            coins.Value = getSave[1]
                rubys.Value = getSave[2]
        else
            local savetable = {
                coins.Value,
                    rubys.Value,
            }
            ds1:SetAsync(plr.UserId, savetable)
        end
    end

    local pass = false

    repeat
    local pass, msg = pcall(getData)
    wait()
    until
    pass

    game.Players.PlayerRemoving:connect(function(plr)
        local savetable = {
            coins.Value,
                rubys.Value,
        }
        ds1:SetAsync(plr.UserId, savetable)
    end)

    game:BindToClose(function()
        local savetable = {
            coins.Value,
                rubys.Value,
    }
        ds1:SetAsync(plr.UserId, savetable)
    end)
end)

game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        if player then
            player:Kick("this game has shutdown")
        end
    end
    wait(5)
end)

This script doesn't save data when I test it in studio, this script disappears from ServerScriptService is there any reason why this script disappears and doesn't save players data?

If you know the answer, please let me know. I'm very confused about why this isn't working.

1 answer

Log in to vote
0
Answered by
sheepposu 561 Moderation Voter
4 years ago

Ok first off, scripts just disappear from the ServerScriptService, it's supposed to happen. They are still the child to the ServerScriptService, but they just aren't there. Next, you shouldn't be declaring a function, inside a function. declare the getData function outside of the first function. Then send paramters through to the getData function. Same goes for the "game.Players.PlayerRemoving" and "BindToClose". In the BindToClose outside of the "Player.PlayerAdded" function, save their data and then kick them, so that the other one inside the playeradded function is not needed. One last thing - any errors?

0
There weren’t really any errors and I’m new to scripting so I’m not sure how I would change my script to match what you are saying Vxpper 101 — 4y
Ad

Answer this question