I am changing a "Points" IntValue in leaderstats via server side scripts and client side, but the value won't save. It says that it saved data successfully, but my points value is the same as before. The odd thing is, it worked before, and saved my points value, but now it will not. Any help? Also, I am just changing the IntValue when I need to change the points. Is this the correct way to do that? Thanks in advance! :D

code for the leaderstats handler

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("PointsData")

print("started")

game.Players.PlayerAdded:Connect(function(player)
 local leaderstats = Instance.new("Folder", player)
 leaderstats.Name = "leaderstats"

 local points = Instance.new("IntValue",leaderstats)
 points.Name = "Points"

 local data
 local success, errormessage = pcall(function()
	data = ds:GetAsync(player.UserId.."-points")
end)
 
 if success then
	print("got data")
	points.Value = data
else
	print("could not get data")
	warn(errormessage)
end

end)


game.Players.PlayerRemoving:Connect(function(player)
 
	local success, errormessage = pcall(function()
		ds:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
	end)
	
	if success then
		print("Points saved successfully.")
	else
		print("Error saving data.")
		warn(errormessage)
	end
	
end)

2 comments

0

Brand new to this great community and very green at scripting, but I strugged with a very similar issue, and found that my mechanism for updating the value (Points in your case) was only happening on the client side; on the server the value remained unchanged, so the datastore update was storing the unchanged, server value.

0

Ahh. ok thanks! I will test it out.