Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Value of my IntValue object changes but when I reference it in a scrip it still shows 0?

I put strength, intelligence, defense, and health stat values in a folder in the player call playerstats and in another local script when a player levels up and adds points to that stat its suppose to go up which it does when I check the value, but when I try to save it using a script in serverscriptservice the values show up as 0.

local function savePointAllocation(player)
	local playerstats = player.PlayerStats
	local str = playerstats:FindFirstChild("Strength")
	local def = playerstats:FindFirstChild("Defense")
	local int = playerstats:FindFirstChild("Intelligence")
	local hp = playerstats:FindFirstChild("Health")
	local success, err = pcall (function()
		local newStatValues = {
				Strength = str.Value,
				Defense = def.Value,
				Intelligence = int.Value,
				Health = hp.Value 
		}
		plrStats:SetAsync(player.UserId.."Stats",newStatValues)	
	end)
	
	if success then
		print("Point allocation successfully saved!")
	elseif not success then
		repeat
			print(err)
			local newStatValues = {
				Strength = str.Value,
				Defense = def.Value,
				Intelligence = int.Value,
				Health = hp.Value 
			}
			plrStats:SetAsync(player.UserId.."Stats",newStatValues)
		until success
	end
end
game.Players.PlayerRemoving:Connect(function(player)
	savePointAllocation(player)
end)

2 comments

0

Is this a localscript?

0

its a script inside the serverscripservice

1 answer

The classic you forgot game:BindToClose().

Basically before your datastore can successfully store all data the game server has already shutdown and terminates the datastore process. However if you use game:BindToClose(), loop through all remaining players in the game and save the data for each of the players at that point, then the game will wait until the saving has been handled and then exit the game.

The wiki explains this exact issue aswell:

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

1 comment

0

the game server isn't shutting down and not saving, it's that when it gets the values from the player stats to save it in a datastore it gets 0 even if the value is not 0

This discussion is preserved as it was. New answers, comments, and votes are closed.