I got an error with nil and I dont know how to solve it

Hello I get a one issue with my code

code:
local tool = script.Parent

local player = game.Players.LocalPlayer

local char = player.Character

local humanoid = char:WaitForChild(“Humanoid”)

tool.Equipped:Connect(function()

if humanoid then

humanoid.WalkSpeed = 50

end

end)

tool.Unequipped:Connect(function()

if humanoid then

humanoid.WalkSpeed = 16

end

end)

that script has to change player walk speed when item is equipped to 50

issue its dont work because of something with nil and waitforchild

Error Message:
Players.vitopencilPro2020.Backpack.Bumper Car.LocalScript:4: attempt to index nil with ‘WaitForChild’ - Client - LocalScript:4
17:17:13.730 Stack Begin - Studio
17:17:13.730 Script ‘Players.vitopencilPro2020.Backpack.Bumper Car.LocalScript’, Line 4 - Studio - LocalScript:4
17:17:13.730 Stack End - Studio
17:31:47.803 Disconnect from ::ffff:127.0.0.1|55433 - Studio

one time its work and changed my walk speed but next time its didnt work I dont try any solutions because I dont know what I can do

Im trying to do something with it but its always dont work

1 Like

Please format your code, also check if the character exists, and if it does not, then wait for it, like so:

char = player.Character or player.CharacterAdded:Wait()
1 Like

Use this insted of this local char = player.Character:

local char = player.Character or player.CharacterAdded:Wait()
1 Like

Also like @DasKairo said to create code format like this:

code

You can press contrl + E in new line or press button number 6 in new line, this button looks like this: </>. Also you can just put this ``` in start and end, in center put code you want. It makes code more readable.

1 Like