Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)`

1 comment

0

heh that looks like a lvl up script I made

1 answer

✓ Accepted answer

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!

2 comments

0

Oh thanks I didn't notice!

0

No problem, good luck developing!

This discussion is preserved as it was. New answers, comments, and votes are closed.