Error attempt to index nil with 'WaitForChild'

Hello! I’m making an item that you can throw and it will deal damage to npc’s. If the player picks the item up while its in workspace, the script works fine, but if I put the item in StarterPack the script stops working and I get this error:

Here is the local script:

local plr = game.Players.LocalPlayer
local cooldown = false
local humanoid = plr.Character:WaitForChild("Humanoid") -- Where the error occurs
local animation = humanoid:LoadAnimation(script:WaitForChild("ThrowAnimation"))

script.Parent.Activated:Connect(function()
	if cooldown == false then
		cooldown = true
		animation:Play()
		script.ThrowEvent:FireServer( )
		wait(1) --time for cooldown
		cooldown = false
	end
end) 

Any help is appreciated!

1 Like

See if adding a WaitForChild before the plr when getting the character like this:

local char = plr:WaitForChild("Character")
local humanoid = char:WaitForChild("Humanoid")

sometimes it isn’t loaded in

1 Like

I am sure the problem comes from the character element which has not loaded in yet
define this variable for the character

local Character = plr.Character or plr.CharacterAdded:Wait()

2 Likes

Thanks for helping! I’ve been trying to fix this for a few hours. Thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.