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

Tools datastore not saving upon PlayerRemoving?

Asked by 4 years ago
Edited 4 years ago

Hello, so I've been trying to save upon playerremoving but it never wants to work. I even used BindToClose() and I know it works 30 seconds before shutdown they said. But when I leave, the tool wont save.

This is currently my code right now:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
    pcall(function()
        local tool = dataStore:GetAsync("User-"..player.UserId)
        if tool then
            for i,v in pairs(tool) do
                local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
                if toolFound  then
                    toolFound:Clone().Parent = player.Backpack
                end
            end
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
    local toolsSave = {}

        for i, tool in pairs (player.Backpack:GetChildren()) do
            if tool then
                table.insert(toolsSave,tool.Name)
            end
        end
        dataStore:SetAsync("User-"..player.UserId,toolsSave)
end)
end)

game:BindToClose(function()


    for _, player in pairs(game.Players:GetPlayers()) do -- notice that you can loop through every player left in the server so the function can run for them
        local toolsSave = {}
        for i, tool in pairs (player.Backpack:GetChildren()) do
            if tool then
                table.insert(toolsSave,tool.Name)
            end
        end
        dataStore:SetAsync("User-"..player.UserId,toolsSave)
    end
end)

Any help is appreciated...

0
https://developer.roblox.com/en-us/articles/Datastore-Errors Wait 6 seconds between each request User#30567 0 — 4y

2 answers

Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
4 years ago

your character is probably spawning after you parent the tool to the backpack, meaning that your backpack gets reset

try parenting a clone of the tool to both player.Backpack and player.StarterGear instead of just player.Backpack

Ad
Log in to vote
0
Answered by 4 years ago

It worked fine for me. Are you sure you have API Services enabled? (Game Settings -> Security -> Enable Studio Access to API Services)? Make sure to restart your studio after you save.

0
Apparently, I get "22:58:38.509 - DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = User-28814494" WARN, which I'm aware it happens. But I feel like there's something sketchy in my code. TinfoilbotGamer 35 — 4y
0
So its not working, and yes I enabled API services. TinfoilbotGamer 35 — 4y
0
did you press save and restart studio? NotMiskie 78 — 4y
0
Apparently, it just rarely works. Any ways to fix this? TinfoilbotGamer 35 — 4y
0
I would recommend making it also auto-save every few seconds since data stores fail saving on player removed especially when there's only one player in the server NotMiskie 78 — 4y

Answer this question