For some reason this error comes up any help? attempt to index upvalue ‘Xpn’ (a number value)
01 | local plr = game.Players.LocalPlayer |
02 |
03 | local Xp = plr:WaitForChild( "XP" ) -- Using WaitForChild() so the script won't break if "XP" has not loaded yet. |
04 |
05 | local lvl = plr:WaitForChild( "lvl" ) |
06 |
07 | local Xpn = plr:WaitForChild( "XpNeeded" ) -- How much more xp does a person need |
08 |
09 | |
10 |
11 | lvl.Changed:Connect( function () |
12 |
13 | if lvl.Value = = 2 then -- How much Xp needed will get updated |
14 |
15 | Xpn = Xpn.Value+ 50 |
16 |
17 | end |
18 |
19 | end ) |
20 |
21 | |
22 |
23 | lvl.Changed:Connect( function () |
24 |
25 | if lvl.Value = = 3 then -- How much Xp needed will get updated |
26 |
27 | Xpn = Xpn.Value+ 75 |
28 |
29 | end |
30 |
31 | end ) |
32 |
33 | |
34 |
35 | lvl.Changed:Connect( function () |
36 |
37 | if lvl.Value = = 4 then -- How much Xp needed will get updated |
38 |
39 | Xpn = Xpn.Value+ 100 |
40 |
41 | end |
42 |
43 | end ) |
44 |
45 | |
46 |
47 | lvl.Changed:Connect( function () |
48 |
49 | if lvl.Value = = 5 then -- How much Xp needed will get updated |
50 |
51 | Xpn = Xpn.Value+ 125 |
52 |
53 | end |
54 |
55 | end ) |
56 |
57 | |
58 |
59 | Xp.Changed:Connect( function () |
60 |
61 | if Xp.Value > = Xpn.Value then |
62 |
63 | Xp.Value = Xp.Value-Xpn.Value |
64 |
65 | lvl.Value = lvl.Value+ 1 |
66 |
67 | end |
68 |
69 | end )` |
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:
01 | local plr = game.Players.LocalPlayer |
02 | local Xp = plr:WaitForChild( "XP" ) -- Using WaitForChild() so the script won't break if "XP" has not loaded yet. |
03 | local lvl = plr:WaitForChild( "lvl" ) |
04 | local Xpn = plr:WaitForChild( "XpNeeded" ) -- How much more xp does a person need |
05 |
06 | lvl.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 |
22 | end ) |
23 |
24 | Xp.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 |
29 | end ) |
If I helped you, then be sure to upvote and accept my answer!
Happy scripting!