As far as I can tell, this code works properly. The problem could potentially be that your updating leaderstat values from a LocalScript. If this is the issue, convert the code into a script, put the script into ServerScriptService and place your code in a game.Players.PlayerAdded:Connect(function())
EDIT: Depending on the method of updating, it may be best to combine the script I suggested you make and the leaderstats into one script.
Never save data with the .Changed event, as it can throttle if the value changes too often.
Only save every X seconds, when the player leaves and set up a BindToClose function.
Wrap your calls in a pcall():
This is used for error handling.
It will execute the function, and if the function errors, it will handle it and give you the error, rather than erroring in your main thread and stopping.
What is the purpose of this?
local folder = Instance.new("Folder") or player.leaderstats
It should look like this:
local folder = Instance.new("Folder")
And, NEVER use the parent argument that comes with Instance.new(), as this is slower.
Assign the properties first, then parent it.