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:

while true do
local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character or Player.CharacterAdded:wait()
local h = Player.Character:FindFirstChild("Humanoid")
        if h.WalkSpeed == 16 then
            h.JumpPower = 50
        elseif h.WalkSpeed >= 16 and h.WalkSpeed <= 267 then
            h.JumpPower = 100
        elseif h.WalkSpeed >= 268 and h.WalkSpeed <= 514 then
            h.JumpPower = 150
        elseif h.WalkSpeed >= 515 and h.WalkSpeed <= 767 then
            h.JumpPower = 175
        elseif h.WalkSpeed > 767 and h.WalkSpeed <= 1000 then
            h.JumpPower = 200
        elseif h.WalkSpeed > 1000 then
            h.JumpPower = 250
        end
        wait(0.5)
        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.

while true do
    local Player = game:GetService("Players").LocalPlayer
    local Char = Player.Character or Player.CharacterAdded:wait()
    local h = Char:WaitForChild("Humanoid")-- Added WaitForChild
    if h.WalkSpeed == 16 then
        h.JumpPower = 50
    elseif h.WalkSpeed >= 16 and h.WalkSpeed <= 267 then
        h.JumpPower = 100
    elseif h.WalkSpeed >= 268 and h.WalkSpeed <= 514 then
        h.JumpPower = 150
    elseif h.WalkSpeed >= 515 and h.WalkSpeed <= 767 then
        h.JumpPower = 175
    elseif h.WalkSpeed > 767 and h.WalkSpeed <= 1000 then
        h.JumpPower = 200
    elseif h.WalkSpeed > 1000 then
        h.JumpPower = 250
    end
    wait(0.5)
end

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.

local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character or Player.CharacterAdded:wait()
local h = Char:WaitForChild("Humanoid")

while wait(0.5) do
    if h.WalkSpeed == 16 then
        h.JumpPower = 50
    elseif h.WalkSpeed >= 16 and h.WalkSpeed <= 267 then
        h.JumpPower = 100
    elseif h.WalkSpeed >= 268 and h.WalkSpeed <= 514 then
        h.JumpPower = 150
    elseif h.WalkSpeed >= 515 and h.WalkSpeed <= 767 then
        h.JumpPower = 175
    elseif h.WalkSpeed > 767 and h.WalkSpeed <= 1000 then
        h.JumpPower = 200
    elseif h.WalkSpeed > 1000 then
        h.JumpPower = 250
    end
end
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