Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this script Assign Values script?

Asked by 9 years ago

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)


1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

0
It's not in a Gui, it's just a script in the Workspace. How can I get around the problem with the LocalPlayer? SlickPwner 534 — 9y
1
A LocalScript in the workspace will not run... Really this should be using `PlayerAdded` in a normal Script. I have edited the answer to include a conversion using PlayerAdded. BlueTaslem 18071 — 9y
1
Anywhere a Script would run (which is both of those, I suppose the modern way would be ServerScriptStorage). There's no reference to `script` in here so obviously the code itself is blind to where it runs. BlueTaslem 18071 — 9y
0
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. SlickPwner 534 — 9y
View all comments (4 more)
0
Edited to WaitForDataReady. BlueTaslem 18071 — 9y
0
Doesn't work at all now, not even inserting leaderstats. SlickPwner 534 — 9y
0
It will only work online where Data is available.... BlueTaslem 18071 — 9y
0
That worked, thanks! SlickPwner 534 — 9y
Ad

Answer this question