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

What is wrong with my Assign Values script?

Asked by 10 years ago

I'm rescripting a value assigner for an RPG game, and for some reason it just doesn't want to work. The output gives me: leaderstats is not a valid member of Player gold is not a valid member of Player etc.

Code:

plr = game.Players.LocalPlayer
leaderstats = Instance.new("IntValue")
gold = Instance.new("NumberValue")
gems = Instance.new("NumberValue")
level = Instance.new("NumberValue")
xp = Instance.new("NumberValue")

goldKey = "PlayerGold"
gemsKey = "PlayerGems"
levelKey = "PlayerLevel"
xpKey = "PlayerExp"

function assignValues()
    repeat wait() until plr
    --leaderstats
    leaderstats.Parent = plr
    leaderstats.Name = "leaderstats"
    repeat wait() until 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


game.Players.PlayerAdded:connect(assignValues)

plr.leaderstats.Gold.Changed:connect(saveStats)
plr.leaderstats.Gems.Changed:connect(saveStats)
plr.leaderstats.Level.Changed:connect(saveStats)
plr.Exp.Changed:connect(saveStats)



0
what line is the error on Teeter11 281 — 10y
0
It doesn't tell me. It just says "leaderstats is not a valid member of Player". SlickPwner 534 — 10y
0
it would be blue Teeter11 281 — 10y
0
It's red, not blue. SlickPwner 534 — 10y
View all comments (4 more)
0
its underneath the blue Teeter11 281 — 10y
0
Oh, it's in another script that's part of a Gui then. SlickPwner 534 — 10y
0
but what line Teeter11 281 — 10y
0
It doesn't matter, it's a script that displays stats, the reason that script is failing is because it can't find the stats inside the player. SlickPwner 534 — 10y

1 answer

Log in to vote
0
Answered by
Teeter11 281 Moderation Voter
10 years ago

put the instance.new parts inside the function

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)




i hope ur RPG is good :)

0
Still having the same problem, not sure why. SlickPwner 534 — 10y
Ad

Answer this question