Kill when touch script doesn't work

I’m making a script to when a player touch the block, the player is killed, but isn’t working.

script.Parent.Touched:connect(function(touch)
	local char = touch.Parent
	if not char.Name == script.Parent.Parent.Name then
		char.Humanoid.Health = 0
	end
end)

If statement should be

if char.Name ~= script.Parent.Parent.Name then

Also I recommend you put some checks to make sure what is touching is a Character and not anything else, so your code is better off looking like this to make it a bit more safer

local part = script.Parent

part.Touched:Connect(function(touch)
	local char = touch.Parent
	local hum = char:FindFirstChild("Humanoid")
	if not hum or char.Name == part.Parent.Name then return end
	hum.Health = 0
end)

Didn’t worked ;_;

I’m trying to kill a dummy with the part, but the dummy life is allways 100

I used

because the part is allways touching me and the parent of the part is my character

Is the dummy moving on the part? There needs to be some movement involved for Touched to activate, add a print statement in the event to see if it’s being ran.

I’d recommend adding an extra check to make sure the part you’re touching isn’t a hat.

function GetPlayerFromObj(obj)
    if obj.Parent:FindFirstChild("Humanoid") ~= nil then
        return obj.Parent
   end
    if obj.Parent.Parent:FindFirstChild("Humanoid") ~= nil then
        return obj.Parent.Parent
    end
    return nil
end

I tested now with the dummy walking and worked.

1 Like

Then it was probably due to the event not running.

If you have anymore issues don’t be afraid to make another post!