Data store doesn't get the right values

Hello developers, I am currently working on a data store script. However, the function doesn’t get the right values. Here is the script:

game.Players.PlayerRemoving:Connect(function(player)
	print("player removing")
	local stage = player:WaitForChild("leaderstats"):WaitForChild("Stage")
	local maxLevel = player:WaitForChild("MaxLevel")

	local success, errormsg = pcall(function()
		GeneralDataStore:SetAsync(player.UserId .. "-Stage", stage.Value)
		GeneralDataStore:SetAsync(player.UserId .. "-MaxLevel", maxLevel.Value)
		
		-- prints 0 and 0 whereas their values are different
		print("datas saved with: " .. maxLevel.Value .. "(MAX) " .. stage.Value .. " STAGE") 
	end)

	if errormsg then warn(errormsg) end
end)

is this line working?

print("datas saved with: " .. maxLevel.Value .. "(MAX) " .. stage.Value .. " STAGE") 

yeah, they print out 0 (they shouldn’t)

Are you changing the Values from a local script or a global script?
If its a local script then that’s why its not working.

I change them from a local script

The value should be changed from a global script not a local script as its from the client.

1 Like

Yeah it should now work, thanks!

1 Like

It looks like you’re encountering an issue with your data store script where the values you’re trying to save aren’t being set correctly. I see that you’re using the GeneralDataStore to store a player’s stage and max level values, but these values aren’t being updated as expected. Let’s troubleshoot and see what might be going wrong.

Your code looks fine at a glance, but let’s consider a few possibilities:

  1. Data Store Setup: Make sure that you’re properly initializing your GeneralDataStore. It should be created using the DataStoreService:GetDataStore function with a unique name. Check that you’re creating the data store instance properly before using it.
  2. Player Data Access: Verify that your player’s leaderstats and MaxLevel values are actually being updated during the player’s gameplay. If these values are not updated correctly, it might explain why you’re getting unexpected values when saving them.
  3. Data Store Saving: Since you’re using pcall to save the data, it’s important to check if the pcall block is throwing any errors. The pcall function captures errors that occur within the pcall block and stores them in the errormsg variable. Make sure to check the value of errormsg to see if any errors are being thrown during the data saving process.

I’d recommend adding some additional print statements or debugging messages to narrow down the issue. For example, you can print the maxLevel.Value and stage.Value before and after attempting to save them to see if they are being updated correctly.

If you’re still encountering issues after checking the above points, feel free to share more details about your data store setup, how the player’s data is being updated, and any additional error messages you’re seeing. That way, I can provide more specific guidance to help you troubleshoot the issue. Happy coding! :rocket:

1 Like

Thanks for the reply! It is a really nice one but it’s caused because of a local script as you can see.

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