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 6 years ago

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

01local plr = game.Players.LocalPlayer
02 
03local Xp = plr:WaitForChild("XP") -- Using WaitForChild() so the script won't break if "XP" has not loaded yet.
04 
05local lvl = plr:WaitForChild("lvl")
06 
07local Xpn = plr:WaitForChild("XpNeeded") -- How much more xp does a person need
08 
09   
10 
11lvl.Changed:Connect(function()
12 
13if lvl.Value == 2 then -- How much Xp needed will get updated
14 
15Xpn = Xpn.Value+50
16 
17end
18 
19end)
20 
21   
22 
23lvl.Changed:Connect(function()
24 
25if lvl.Value == 3 then -- How much Xp needed will get updated
26 
27Xpn = Xpn.Value+75
28 
29end
30 
31end)
32 
33   
34 
35lvl.Changed:Connect(function()
36 
37if lvl.Value == 4 then -- How much Xp needed will get updated
38 
39Xpn = Xpn.Value+100
40 
41end
42 
43end)
44 
45   
46 
47lvl.Changed:Connect(function()
48 
49if lvl.Value == 5 then -- How much Xp needed will get updated
50 
51Xpn = Xpn.Value+125
52 
53end
54 
55end)
56 
57   
58 
59Xp.Changed:Connect(function()
60 
61if Xp.Value >= Xpn.Value then
62 
63Xp.Value = Xp.Value-Xpn.Value
64 
65lvl.Value = lvl.Value+1
66 
67end
68 
69end)`
0
heh that looks like a lvl up script I made WyattagumsBackUp 5 — 6y

1 answer

Log in to vote
0
Answered by
awfulszn 394 Moderation Voter
6 years ago
Edited 6 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:

01local plr = game.Players.LocalPlayer
02local Xp = plr:WaitForChild("XP") -- Using WaitForChild() so the script won't break if "XP" has not loaded yet.
03local lvl = plr:WaitForChild("lvl")
04local Xpn = plr:WaitForChild("XpNeeded") -- How much more xp does a person need
05 
06lvl.Changed:Connect(function()
07    if lvl.Value == 2 then
08        Xpn.Value = Xpn.Value + 50
09    end
10 
11    if lvl.Value == 3 then
12        Xpn.Value = Xpn.Value + 75
13    end
14 
15    if lvl.Value == 4 then
16        Xpn.Value = Xpn.Value + 100
17    end
18 
19    if lvl.Value == 5 then
20        Xpn.Value = Xpn.Value + 125
21    end
22end)
23 
24Xp.Changed:Connect(function()
25    if Xp.Value >= Xpn.Value then
26        Xp.Value = Xp.Value - Xpn.Value
27        lvl.Value = lvl.Value + 1
28    end
29end)

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

Happy scripting!

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

Answer this question