Hello, I have two separate data store scripts. One of them is for saving a player's stats, that works just fine.

The second datastore script is for saving items. My game is set up in rounds. When a player first starts the game, their backpack is empty. First round happens, no items. Round ends and player goes to the Lobby. After hitting the Lobby, before the second round begins, their Items appear in their backpack. How can I make it so that the Items appear when the player first spawns into the Lobby instead of having to wait a round?

Here's my Items Datastore script:

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

--Setup the DataStore
local ds = game:GetService("DataStoreService"):GetDataStore("RemovedForSHPost");
local tools = game.ServerStorage.ToolStorage;  --Specify all possible tools


    --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)







--{[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)

In case there's some problem with the two, here's my Stats Datastore script:

game.Players.PlayerAdded:connect(function(player)
		local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do			
	stats[i].Value = datastore:GetAsync(stats[i].Name)
	print("stat number "..i.." has been found")
	
	stats[i].Changed:connect(function() 
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
	datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
	print("saved data number "..i)
	
end
print("Stats successfully saved")	
end)
		end
end)

2 comments

0

I expect the error is due to the CharacterAdded callback being defined after the character has already loaded. Putting your two variables (ds and tools) at the top of your code, not inside the PlayerAdded callback, should fix the problem.

0

I did that, no difference. :(