Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is Datastore failing after a player leaves/returns?

Hello,

My Datastore is not saving the items within the folder I've set up accurately. My game is set up in rounds, player purchases items and they stay in a backpack, round happens/ends, rinse and repeat, no problem. When a player leaves the game and returns, the items are still in the backpack, however, after the first round ends, the items have disappeared. How do I prevent the items from disappearing permanently?

The script below was created with much help from the wonderful devs here, and I'm not quite sure where I've gone wrong below. Thank you for your help!

local ds = game:GetService("DataStoreService"):GetDataStore("Tools");
local tools = game.ServerStorage.ToolStorage;  --Folder created to store Tools.


--{[Check and Load]}
game.Players.PlayerAdded:connect(function(plr)

    --Retrieve the potential data
    local data = ds:GetAsync(plr.UserId);

    --Check if it exists
    if data then

        --If it does, loop and clone respective tools.
        for _,v in next,data do
            local tool = tools:FindFirstChild(v);
            if tool then
                tool:Clone().Parent = plr.Backpack;
            end
        end
    end
end)

--{[Collect and Save]}

game.Players.PlayerRemoving:connect(function(plr)
	
    --Make a table to hold all the data
    local toolList = {};

    --Iterate through the backpack and fill the table in

    for _,v in next,plr.Backpack:GetChildren() do
        --Make sure it's actually a tool

        if v:IsA("Tool") or v:IsA("HopperBin") then
            toolList[#toolList+1] = v.Name;
        end
    end
    --Save the table

    ds:SetAsync(plr.UserId,toolList);
end)


1 answer

✓ Accepted answer

If I were home, I would surely check the code to see if it would be running properly.

The reason why is because in the PlayerAdded function, all the code in it runs ONLY when the player joins. This is where the CharacterAdded comes in handy.

This what everything should look like in the PlayerAdded function.

NOTE: I am not too sure if I am doing this the correct way.

--{[Check and Load]}
game.Players.PlayerAdded:connect(function(plr)

    --Retrieve the potential data
    local data = ds:GetAsync(plr.UserId);

    --Check if it exists
    plr.CharacterAdded:connect(function(char) -- this will run everytime the Player's Character has been added in workspace.
      if data then
          	--If it does, loop and clone respective tools.
          	for _,v in next,data do
              	local tool = tools:FindFirstChild(v);
              	if tool then
                	tool:Clone().Parent = plr.Backpack;
            	end
          	end
      	end
    end)
end)

If this worked, please accept my answer :)

3 comments

0

:D It seems to work! Got a strange issue now to where the items don't load when a player enter, but AFTER the first round they do. Could I add a Wait(3) somewhere to fix that?

0

Nevermind, its not saving them after all. :(

0

Oh, sorry. I wish I knew datastores.

This discussion is preserved as it was. New answers, comments, and votes are closed.