Attempt to index nil with 'Humanoid'

Hi! I’m trying to make an ability that kills the target, however when i try to play an animation on them, it gives me an error,

ServerScriptService.Abilities.NeckSnap:9: attempt to index nil with 'Humanoid' 

Script:

game.ReplicatedStorage.AbilityEvents.NeckSnap.OnServerEvent:Connect(function(Player, Mouse)
	local animation = script.Caster
	local animation2 = script.Victim
	local loadedanim = Player.Character.Humanoid:LoadAnimation(animation)
	local loadedanim2 = Mouse.Parent.Humanoid:LoadAnimation(animation2)
	loadedanim:Play()
	loadedanim2:Play()
	wait(4.9)
	Mouse.Parent.Humanoid:TakeDamage(500000)
	loadedanim:Stop()
	loadedanim2:Stop()	
end)

Here

Mouse.Parent.Humanoid:TakeDamage(500000)

If this is the problem, it should be

Mouse.hit.Parent.Humanoid:TakeDamage(500000)

And here

local loadedanim2 = Mouse.Parent.Humanoid:LoadAnimation(animation2)

Should be

local loadedanim2 = Mouse.hit.Parent.Humanoid:LoadAnimation(animation2)

I always do damage and animations like this though, I never have this error.

mouse.Parent may not be the player character so do it like so

local loadedanim = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(animation)
local loadedanim2 = Mouse.Parent:FindFirstChild("Humanoid"):LoadAnimation(animation2)
if loadedanim and loadedanim2 then
loadedanim:Play()
loadedanim2:Play()
end

And you cant pass the mouse as a parameter, if its the player mouse, pass mouse.hit through the remote, this will make your code work

I pass it through like this in the local script

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local debounce = false 
local event = game.ReplicatedStorage.AbilityEvents.NeckSnap
local root = Player.Character.HumanoidRootPart

Mouse.KeyDown:Connect(function(key, IsTyping)
	if IsTyping then return end
	key:lower()
	if key == 'z' and debounce == false then
		debounce = true
		if (Mouse.Target.Position - root.Position).magnitude <15 and debounce == true and Player.Character.Humanoid.ragdoll.Value == false and not Player.Character.Humanoid.PlatformStand and Player.Character.Humanoid.Health > 0 then
			event:FireServer(Mouse.Target)
			wait(10)
			debounce = false
		end
	end
end)

can you show us the localscript?

Change

event:FireServer(Mouse.Target)

To

event:FireServer(Mouse.hit)

print(Mouse.Target) [Char Limit]

this produced another error.

“hit is not something of cframe”

i dont remember the thing but it said something like that

Which line is that error on?
Chars

hit is not a valid member of CFrame - Server - NeckSnap:9

mouse.Hit indicates CFrame.
https://developer.roblox.com/en-us/api-reference/property/Mouse/Hit

1 Like

change it back to Mouse.Target and add a print in the local script print(Mouse.Target)

I fixed it, I just added

:WaitForChild("Humanoid")

before loading the animation

1 Like