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)
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:
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.
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.
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!