I made a datastore that's saving "leader stats" values. But it's not saving/loading it?

code:

game.Players.PlayerRemoving:Connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore("SaveStats")

local statstorage = player:WaitForChild("Currency"):GetChildren()
for i =  1, #statstorage do
	pcall(function()
	datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
	print("saving..")
	end)
	end
end)


game.Players.PlayerAdded:Connect(function(player)
		local datastore = game:GetService("DataStoreService"):GetDataStore("SaveStats")
	
	player:WaitForChild("Currency")
	wait(1)
	local stats = player:FindFirstChild("Currency"):GetChildren()
	for i = 1, #stats do			
	pcall(function()
	stats[i].Value = datastore:GetAsync(stats[i].Name)
	end)
	print("loading")
	end
end)

6 comments

0

It prints loading two times though..

0

PlayerAdded is before PlayerRemoving. Thats probably why.

1

It's a event. Does not need to be in any specific order.

0

Then, try joining yourself first and then bringing in another player, see if that other players data is loaded.

0

I think it is a event.

0

It dosen't matter if you have one or more players. It should load for everyone.