I've got this LocalScript to assign some Bool and Number Values to the Player once he joins, but nothing is happening. What is the problem here?
wait(2) -- wait for the character to load
goldKey = "PlayerGold"
gemsKey = "PlayerGems"
levelKey = "PlayerLevel"
xpKey = "PlayerExp"
function assignValues()
plr = game.Players.LocalPlayer
leaderstats = Instance.new("IntValue")
gold = Instance.new("NumberValue")
gems = Instance.new("NumberValue")
level = Instance.new("NumberValue")
xp = Instance.new("NumberValue")
--leaderstats
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
--gold and gems
gold.Parent = plr.leaderstats
gold.Name = "Gold"
gold.Value = plr:LoadNumber(goldKey) or 10
gems.Parent = plr.leaderstats
gems.Name = "Gems"
gems.Value = plr:LoadNumber(gemsKey)
--level and xp
level.Parent = plr.leaderstats
level.Name = "Level"
level.Value = plr:LoadNumber(levelKey) or 1
xp.Parent = plr
xp.Name = "Exp"
xp.Value = plr:LoadNumber(xpKey)
end
function saveStats()
repeat wait() until plr
--gold and gems save
plr:SaveNumber(goldKey, plr.leaderstats.Gold.Value)
plr:SaveNumber(gemsKey, plr.leaderstats.Gems.Value)
--level and xp save
plr:SaveNumber(levelKey, plr.leaderstats.Level.Value)
plr:SaveNumber(xpKey, plr.Exp.Value)
end
assignValues()
plr.leaderstats.Gold.Changed:connect(saveStats)
plr.leaderstats.Gems.Changed:connect(saveStats)
plr.leaderstats.Level.Changed:connect(saveStats)
plr.Exp.Changed:connect(saveStats)

8 comments
It's not in a Gui, it's just a script in the Workspace. How can I get around the problem with the LocalPlayer?
A LocalScript in the workspace will not run... Really this should be using
PlayerAddedin a normal Script. I have edited the answer to include a conversion using PlayerAdded.Anywhere a Script would run (which is both of those, I suppose the modern way would be ServerScriptStorage). There's no reference to
scriptin here so obviously the code itself is blind to where it runs.I tried it, and it worked up until the 21st line of your script. It loaded Leaderstats, Gold, but it didn't work on Gold's Value, and didn't insert the other values.
Edited to WaitForDataReady.
Doesn't work at all now, not even inserting leaderstats.
It will only work online where Data is available....
That worked, thanks!