Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why can't hit.Parent:FindFirstChild() find a humanoid?

I'm making an Undertale game. And for the undyne fight, I made the shield she gives you in the game.

I tried to use the .Touched event with :FindFirstChild() to identify if the spear hit the shield. That didn't work because it couldn't find the shield. Then I just changed it to just the .Touched event. And that did actually destroy the spear. But it won't kick the player when the player touches it.

Heres my code:

Spear.Touched:Connect(function(hit)
	
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		
		local Player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		Player:Kick("Pathetic...")
		
	end
	
Spear:Destroy()
		

end)

1 comment

0

Try putting Spear:Destroy() inside of the if humanoid statement so it will destroy it only if the hit is actual human.

2 answers

Howdy!

You should probably check for the :FindFirstChild("Humanoid") before making it a variable. That might get the script all kinds of screwed up. Try what I got below for your script.

Spear.Touched:connect(function(hit)
	if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
		humanoid = hit.Parent:FindFirstChild("Humanoid")
		local Player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		Player:Kick("I agree... you are pathetic...")
	end
	Spear:Destroy()
end)

##If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.

##Be sure to check out the Roblox API Documentation as well for additional reference.

1 comment

0

I found out that the problem wasn't my code. But since I had both of my meshes (Spear and Shield) anchored, it wouldn't fire the .Touched event. Thank you for your help but I don't think that's the answer.


Spear.Touched:Connect(function(hit)

    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then

        local Player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Instead of saying humanoid.Parent, write hit.Parent.
        Player:Kick("Pathetic...")

    end

Spear:Destroy()


end)

Hope this helps.

This discussion is preserved as it was. New answers, comments, and votes are closed.