Why is datastore not working with no errors

i have a leaderstat and datastore script and 3 of the intvalue leaderstats work for saving but not the stringvalue stat, am i doing something wrong:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local dataStore = DataStoreService:GetDataStore("PlayerStats")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local speed = leaderstats:WaitForChild("Speed")
	local strength = leaderstats:WaitForChild("Strength")
	local endurance = leaderstats:WaitForChild("Endurance")
	local aura = leaderstats:WaitForChild("Aura")

	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)

	if success and data then
		speed.Value = data.Speed or 16
		strength.Value = data.Strength or 0
		endurance.Value = data.Endurance or 0
		aura.Value = data.aura or "None"
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local speed = leaderstats:FindFirstChild("Speed")
		local strength = leaderstats:FindFirstChild("Strength")
		local endurance = leaderstats:WaitForChild("Endurance")
		local aura = leaderstats:WaitForChild("Aura")
		

		local data = {
			Speed = speed and speed.Value or 16,
			Strength = strength and strength.Value or 0,
			Endurance = endurance and endurance.Value or 0,
			Aura = aura and aura.Value or "None"
			
		}

		local success, err = pcall(function()
			dataStore:SetAsync(player.UserId, data)
		end)

		if not success then
			warn("Failed to save data for player " .. player.Name .. ": " .. err)
		end
	end
end)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local speed = leaderstats:FindFirstChild("Speed")
			local strength = leaderstats:FindFirstChild("Strength")
			local endurance = leaderstats:FindFirstChild("Endurance")
			local aura = leaderstats:WaitForChild("Aura")

			local data = {
				Speed = speed and speed.Value or 16,
				Strength = strength and strength.Value or 0,
				Endurance = endurance and endurance.Value or 0,
				Aura = aura and aura.Value or "None"
			}

			local success, err = pcall(function()
				dataStore:SetAsync(player.UserId, data)
			end)

			if not success then
				warn("Failed to save data for player " .. player.Name .. ": " .. err)
			end
		end
	end
end)

When you’re saving, it’s capitalised. But here you use it as a lower case key. Just a simple typo.

2 Likes

If you’re not experienced with datastore yet, i recommend to use a pre-made datastore module such as Profile Service.

Your datastore system is not working due to a heavy lack of safety measures that prevent dataloss, currupted data ect…

1 Like

That isn’t their issue. Their system works fine, just a simple error when typing.

1 Like

thx a lot for helping me fix it, i didnt notice the typo

2 Likes

Their system is pretty unstable and isn’t going to work in a long run.

1 Like

im not experienced with it, but its not corrupted…

1 Like

I mean, your datastore script is missing of a lot of safety measures such as the following:

  • There is no retry in case the save or load datas failed which result into a dataloss.

  • You’re not checking if player datas were correctly loaded before saving which result in a dataloss due to saving default datas when loading failed.

  • You’re not checking if player datas were correctly saved before loading which result in loading the datas of 2 play sessions ago instead of the last one.

  • A sanity check is missing between BindToClose and PlayerRemoving, which lead into saving each player datas twice which could exceed the “SetAsync” call limit and some players could lose their datas.

  • You’re not using coroutine on BindToClose while it is heavily required as you only have 30 seconds to save all players data before the server get entirely closed.

In its current state, there is over 60% chances for a player to lose its datas at any time.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.