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)
As documented on the Wiki SaveNumber
and LoadNumber
have to run from normal Script objects, not LocalScripts.
This really belongs in a regular Script over a LocalScript anyway. The contents will not have to change at all except to redefine plr
in terms a of a script.Parent.Parent...
if this is inside the PlayerGui or PlayerPack.
Here's a Server Script that uses the PlayerAdded
event:
wait(2) -- wait for the character to load goldKey = "PlayerGold" gemsKey = "PlayerGems" levelKey = "PlayerLevel" xpKey = "PlayerExp" function assignValues(plr) plr:WaitForDataReady(); 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) gold.Changed:connect(function() saveStats(plr) end) gems.Changed:connect(function() saveStats(plr) end) level.Changed:connect(function() saveStats(plr) end) xp.Changed:connect(function() saveStats(plr) end) end function saveStats(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 game.Players.PlayerAdded:connect(assignValues);
Please mark this answer as accepted if it addressed all of the so far stated questions.
EDIT: We have to wait for data to be ready before attempting to save or load data.