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

Why doesn't my data save everytime? And why does it only work inGame and not in Studio?

Asked by 4 years ago
players.PlayerRemoving:Connect(function(plr)

local success, errormessage = pcall(function()
    playerData:SetAsync(plr.UserId.."-rep", plr.leaderstats.Reputation.Value)
    end)

    if success then
        print(plr.Name.. " successfully saved.")
    else
        print("There was an error saving "..plr.Name.."'s".." data.")
        warn(errormessage)
    end
end)

Problem

I've been doing some research on data saving and watched some tutorials. This block of code seems to work, but only inGame; not on studio. I made sure to have API services on for studio so that's not the issue. I've seen it run ONCE in studio and never again since. Is it because it's unable to get the data before I exit test mode?

I do plan on having other saving methods, but I'd think having a final last save on leave would be a good idea too if possible.

Question

Why doesn't data save on leave ONLY in studio? Should it be of any concern since it works inGame as intended?

1 answer

Log in to vote
4
Answered by
appxritixn 2235 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

I have seen similar questions asked before.

Typically, I tell people to use the BindToClose() method on game.

game:BindToClose(function()
    -- go through all players, saving their data
    local players = game:GetService("Players"):GetPlayers()
    for _, player in pairs(players) do
        local success, res = pcall(function()
            -- Using 
            playerData:SetAsync(userId, data)
        end)
    if not success then
        warn(res) -- Output the error
    end
    end
end)

This will ensure that before the game server closes, all remaining players have their information saved.

For more information, you can follow this link to the roblox API: https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

As always, if this helped, please accept the answer as it helps me out.

EDIT:

As for why it doesn't work in studio, I am not sure. Why do you want it to work in studio anyway?

Ad

Answer this question