How do I make a part kill only the players, and not the models with a humanoid inside?

Hello, I wanted to make a part kill only the players, but it also kills the models with a humanoid inside, how do i fix this?

The script: (I’m a bad scripter)

function onTouch(part) 
	local humanoid = game.Players.PlayerAdded:FindFirstChild("Humanoid")
	if (humanoid ~= nil) then
		humanoid.Health = humanoid.Health - 10
	end 
end

script.Parent.Touched:connect(onTouch)

You can use Players:GetPlayerFromCharacter(Object.Parent) to get the player instance.
Then you’ll need to define the Player’s character and humanoid.

Basically what was said by Syclya:

function onTouch(part)
    if part.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(part.Parent) then
        part.Parent.Humanoid:TakeDamage(10)
    end
end

it doesn’t work when i touch the part

Im guessing you didn’t add the connection back?

function onTouch(part)
    if part.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(part.Parent) then
        part.Parent.Humanoid:TakeDamage(10)
    end
end

script.Parent.Touched:Connect(onTouch)
1 Like

oh and also, how can I add a 2 seconds delay into this script?

This one will always have it occur after 2 seconds, even if the player stops touching the part:

function onTouch(part)
    task.wait(2) -- Doesn't matter if you use task.wait or wait.
    if part.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(part.Parent) then
        part.Parent.Humanoid:TakeDamage(10)
    end
end

script.Parent.Touched:Connect(onTouch)