Attempt to index nil with Humanoid

I am trying to make like a humanoid anchored system when charging while you fire a skill.The Script is in a tool and is a local script but it still gives a error whenever i try to anchor humanoid.

Players.adcrs.Backpack.Ap Shot.LocalScript:21: attempt to index nil with 'HumanoidRootPart' -

local debounce =  false
local x = false
local eve = game.ReplicatedStorage.ApSHOT
local equip = false
local character = game.Players.LocalPlayer.Character
local MOUSE =game.Players.LocalPlayer:GetMouse()
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
	if equip ==  true then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			x =  true
			character.HumanoidRootPart.Anchored  = true
		end
	end
end)
UIS.InputEnded:Connect(function(input)
	if equip ==  true then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			x = false
			debounce = true
			character.HumanoidRootPart.Anchored  = false
			eve:FireServer(MOUSE.Hit.p)
			wait(5)
			debounce = false
		end
	end
end)
script.Parent.Equipped:Connect(function()
	equip = true
	
	

end)

script.Parent.Unequipped:Connect(function()
	equip = false
end)

Help will be appricated :happy1:

1 Like

Most likely the character may not have loaded by the time the script was ran, change this to

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

So it waits for the Character to be added if it wasn’t already

6 Likes