DataStore dosen't load in data

When i rejoin after my “Money” and “Level” has saved, it dosen’t load in the data. I can’t seem to find the problem.

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Money")
local levelStore = DS:GetDataStore("Level")

local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

game.Players.PlayerAdded:Connect(function(player)
	
	local moneyValue
	local levelValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
		levelValue = levelStore:GetAsync("Player-"..player.UserId)
	end)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	
	if success then
		money.Value = moneyValue
		level.Value = levelValue
	else
		print("Failed to load data")
	end
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
		levelStore:SetAsync("Player_"..player.UserId, player.leaderstats.Level.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to save data")
	end
end

local function autosave()
	while wait (30) do
		for i, players in pairs(game:GetService("Players"):GetPlayers()) do
			save(players)
		end	
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

spawn (autosave)

game.Players.PlayerRemoving:Connect(function()
	save()
end)
1 Like

I’ve just started getting this problem with a script that previously worked.
Unsure as to whether I broke something, but I haven’t made any changes so this seems odd to me.

Exaclty the same. It used to work, and then it just stopped working.

1 Like

Outage? Im going to warn my players that they might lose their data. I feel like this is the same issue as the last data store incident.

1 Like

You’re not passing the player as a parameter.

Welcome to our community! I think you’ll like it here.

Just taking a glimpse of the code, I found something that could be an addition to the problem that you’re having.

game.Players.PlayerRemoving:Connect(function()
	save() -- Where is the player? The function is supposed to call save(player)
end)

You can easily fix this by changing it to

game.Players.PlayerRemoving:Connect(function(player) -- Don't forget to mention the player!
	save(player) -- I added it here for you.
end)

I will read through it and let you know if I find anything else.

I fixed it, but the problem is not gone.

Seems to be working for me again (i didnt need to edit my script though, and anybody who joined during the time it wasnt working lost their data). Not sure what happened, either a hiccup in the service or just an error on my end.

Okay thanks. I will check again tomorrow.