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? ;-;
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')
.
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
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 hope I helped!
Good Luck!