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

Data doesn't save upon rejoining, anyone knows why?

Asked by 3 years ago
local dataStoreService = game:GetService("DataStoreService")
local killSave = dataStoreService:GetDataStore("PlayerKills")

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

    local totalKills
    local success, err = pcall(function()
        totalKills = killSave:GetASync("Player_"..player.UserId)
    end)

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

    local kills = Instance.new("IntValue")
    kills.Name = "Kills"
    if success then
        kills.Value = totalKills
    else
        kills.Value = 0
    end
    kills.Parent = leaderstats

    local rank = Instance.new("StringValue")
    rank.Name = "Rank"
    rank.Value = "Human"
    rank.Parent = leaderstats

    local function updateKills(newKills)
        if newKills then
            if newKills >= 25 then
                rank.Value = "Supreme"
            end
            if newKills >= 100 then
                rank.Value = "Sage"
            end
        end
    end

    updateKills()
    kills.Changed:Connect(updateKills)

end)

local function save(player)
    local success, err = pcall(function()
        killSave:SetAsync("Player_"..player.UserId, player.leaderstats.Kills.Value)
    end)
    if success then
        print("saved successfully")
    else
        print("failed to save")
    end
end

local function autosave()
    while wait(15) do
        for i, player in pairs(game:GetService("Players"):GetPlayers()) do
            print("saving")
            save(player)
        end
    end
end

spawn(autosave)

game.Players.PlayerRemoving:Connect(function(player)
    save(player)
end)

I have API Services allowed so I have no clue what causes the data to not store.

0
Are you only testing on roblox studio? I had a similar problem where data wont save but when I tested in the actual game it suddenly worked. Rayguyban2 89 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The PlayerRemoving event is, in some cases, not quite enough to save all data, as the server closing itself will kill this script when it tries to save the data. Sure, you are hardly saving much with that save() function, but saving to datastores can take a really long time as it takes a long time to communicate with the servers. I resolved this problem myself by using the methods in this tutorial, where it is explained that you should use :BindToClose() in addition to PlayerRemoving. Hopefully this helps!

Edit: here is exactly how I do this in my game.

game.Players.PlayerRemoving:Connect(function()
    SaveData(Player)
end)

game:BindToClose(function()
    SaveData(Player)
end)
0
Also also; this fix SHOULD work 100% of the time, and if you expect the data to always save when a player leaves, then there is no need for that autosave function you have. And use coroutines instead for the future! Galvarino 25 — 3y
0
You have to define Player. You can't just say that, you have to loop through game.Players:GetPlayers() and save for each player. rabbi99 714 — 3y
0
Didn't work, could you try to edit the script for me if I did something wrong because I'm new to scripting and still make a lot of mistakes. Vbrostiic 0 — 3y
Ad

Answer this question