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

'A nil value'?

Asked by
JJ_B 250 Moderation Voter
8 years ago

I made a local script that changes your jump power based on your character's walkspeed, which works in Studio Solo Mode, however in game it gives me the error: Players.JJ_B.Backpack.jump:6: attempt to index local 'h' (a nil value) I've had this sort of problem before but I don't know how to rectify it in the context of this script. Here is the code:

01while true do
02local Player = game:GetService("Players").LocalPlayer
03local Char = Player.Character or Player.CharacterAdded:wait()
04local h = Player.Character:FindFirstChild("Humanoid")
05        if h.WalkSpeed == 16 then
06            h.JumpPower = 50
07        elseif h.WalkSpeed >= 16 and h.WalkSpeed <= 267 then
08            h.JumpPower = 100
09        elseif h.WalkSpeed >= 268 and h.WalkSpeed <= 514 then
10            h.JumpPower = 150
11        elseif h.WalkSpeed >= 515 and h.WalkSpeed <= 767 then
12            h.JumpPower = 175
13        elseif h.WalkSpeed > 767 and h.WalkSpeed <= 1000 then
14            h.JumpPower = 200
15        elseif h.WalkSpeed > 1000 then
16            h.JumpPower = 250
17        end
18        wait(0.5)
19        end

Help please? ;-;

2 answers

Log in to vote
2
Answered by 8 years ago

This seems to me that this is a matter of replication of the character not being complete by the time the CharacterAdded event is fired, so to the server the full character is there but to the client as little as the character model may be there at the time. If this is the cause of the error then it may be fixed by changing FindFirstChild('Humanoid') on line 4 to WaitForChild('Humanoid').

0
Oh, it was so simple! Thanks JJ_B 250 — 8y
0
Beat me to it :P User#11440 120 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

You're not waiting for the Humanoid to load!

When defining variables, it's often appropriate, or necessary, to use WaitForChild.

The very simple fix would be the following.

01while true do
02    local Player = game:GetService("Players").LocalPlayer
03    local Char = Player.Character or Player.CharacterAdded:wait()
04    local h = Char:WaitForChild("Humanoid")-- Added WaitForChild
05    if h.WalkSpeed == 16 then
06        h.JumpPower = 50
07    elseif h.WalkSpeed >= 16 and h.WalkSpeed <= 267 then
08        h.JumpPower = 100
09    elseif h.WalkSpeed >= 268 and h.WalkSpeed <= 514 then
10        h.JumpPower = 150
11    elseif h.WalkSpeed >= 515 and h.WalkSpeed <= 767 then
12        h.JumpPower = 175
13    elseif h.WalkSpeed > 767 and h.WalkSpeed <= 1000 then
14        h.JumpPower = 200
15    elseif h.WalkSpeed > 1000 then
16        h.JumpPower = 250
17    end
18    wait(0.5)
19end

I also could offer some advice to make your script more efficient.

Move your variables outside the loop, so they don't cause lag from repeatedly being created.

01local Player = game:GetService("Players").LocalPlayer
02local Char = Player.Character or Player.CharacterAdded:wait()
03local h = Char:WaitForChild("Humanoid")
04 
05while wait(0.5) do
06    if h.WalkSpeed == 16 then
07        h.JumpPower = 50
08    elseif h.WalkSpeed >= 16 and h.WalkSpeed <= 267 then
09        h.JumpPower = 100
10    elseif h.WalkSpeed >= 268 and h.WalkSpeed <= 514 then
11        h.JumpPower = 150
12    elseif h.WalkSpeed >= 515 and h.WalkSpeed <= 767 then
13        h.JumpPower = 175
14    elseif h.WalkSpeed > 767 and h.WalkSpeed <= 1000 then
15        h.JumpPower = 200
16    elseif h.WalkSpeed > 1000 then
17        h.JumpPower = 250
18    end
19end
I moved the variables and moved the wait to the while loop. I also used the char variable you made but didn't use for some reason.

I hope I helped!

Good Luck!

Answer this question