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

Why does this error pop up - attempt to index upvalue 'Xpn' (a number value)?

Asked by 5 years ago

For some reason this error comes up any help? attempt to index upvalue 'Xpn' (a number value)

local plr = game.Players.LocalPlayer

local Xp = plr:WaitForChild("XP") -- Using WaitForChild() so the script won't break if "XP" has not loaded yet.

local lvl = plr:WaitForChild("lvl")

local Xpn = plr:WaitForChild("XpNeeded") -- How much more xp does a person need



lvl.Changed:Connect(function()

if lvl.Value == 2 then -- How much Xp needed will get updated

Xpn = Xpn.Value+50

end

end)



lvl.Changed:Connect(function()

if lvl.Value == 3 then -- How much Xp needed will get updated

Xpn = Xpn.Value+75

end

end)



lvl.Changed:Connect(function()

if lvl.Value == 4 then -- How much Xp needed will get updated

Xpn = Xpn.Value+100

end

end)



lvl.Changed:Connect(function()

if lvl.Value == 5 then -- How much Xp needed will get updated

Xpn = Xpn.Value+125

end

end)



Xp.Changed:Connect(function()

if Xp.Value >= Xpn.Value then

Xp.Value = Xp.Value-Xpn.Value

lvl.Value = lvl.Value+1

end

end)`
0
heh that looks like a lvl up script I made WyattagumsBackUp 5 — 5y

1 answer

Log in to vote
0
Answered by
awfulszn 394 Moderation Voter
5 years ago
Edited 5 years ago

Essentially, you're setting Xpn (A number value) equal to the value of itself, when you should be setting the Value of Xpn to itself.

Additionally, you are using more than one .Changed for the same value, which is unneeded.

Revised code:

local plr = game.Players.LocalPlayer
local Xp = plr:WaitForChild("XP") -- Using WaitForChild() so the script won't break if "XP" has not loaded yet.
local lvl = plr:WaitForChild("lvl")
local Xpn = plr:WaitForChild("XpNeeded") -- How much more xp does a person need

lvl.Changed:Connect(function()
    if lvl.Value == 2 then
        Xpn.Value = Xpn.Value + 50
    end

    if lvl.Value == 3 then
        Xpn.Value = Xpn.Value + 75
    end

    if lvl.Value == 4 then
        Xpn.Value = Xpn.Value + 100
    end

    if lvl.Value == 5 then
        Xpn.Value = Xpn.Value + 125
    end
end)

Xp.Changed:Connect(function()
    if Xp.Value >= Xpn.Value then
        Xp.Value = Xp.Value - Xpn.Value
        lvl.Value = lvl.Value + 1
    end
end)

If I helped you, then be sure to upvote and accept my answer!

Happy scripting!

0
Oh thanks I didn't notice! MaciBoss1950 16 — 5y
0
No problem, good luck developing! awfulszn 394 — 5y
Ad

Answer this question