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

Part touched connect function (???)

I Just did something like this,

part.Touched:Connect(function(p)
	if p.Parent.FindFirstChild == ("Humanoid") then 
		
		local plr = workspace[p.Parent.Name]
		
		wait(0.5)
			plr.Humanoid:TakeDamage(20)
		wait(0.1)
		part:Destroy()
		
					
	end
			
end)

end)

but i dont know what did i do wrong can someone help please?

script.Parent.Activated:Connect(function()
	
local character = script.Parent.Parent
local Humanoid = character:WaitForChild('Humanoid')
local torso = character:WaitForChild('HumanoidRootPart')
	
local part = Instance.new("Part",game.Workspace)
part.Size = Vector3.new(7.8, 3.6, 4.2)
part.CanCollide = false
part.Anchored = true
part.CFrame = torso.CFrame+(torso.CFrame.lookVector*3)

part.Touched:Connect(function(p)
	if p.Parent.FindFirstChild == ("Humanoid") then 
		
		local plr = workspace[p.Parent.Name]
		
		wait(0.5)
			plr.humanoid.Health = plr.humanoid.Health - 20
		wait(0.1)
		part:Destroy()
		
					
	end
			
end)

end)

Full code is this, other things are working but that part.Touched is broken

2 answers

✓ Accepted answer

Easy mistake, FindFirstChild = ("Humanoid") doesn't work because you are asking to find the first child and equalizing it with "Humanoid"

Just copy this code, and you'll be on your way.

I also recommend using "FindFirstChildOfClass" for the best humanoid detection, but remove that if you don't want it.

part.Touched:Connect(function(p)
	    if p.Parent.FindFirstChildOfClass("Humanoid") then
 
	        local plr = workspace[p.Parent.Name]
	 
	        wait(0.5)
	            plr.humanoid.Health = plr.humanoid.Health - 20
	        wait(0.1)
	        part:Destroy()
 
 	    end	 
	end)

3 comments

0

Its worked but its killing instantly, i want do to once 20 when u Activated the tool

0

And the part is killing me too i just want to kill custom models like zombies and not players. Should i change Humanoid name or something what i have to do for it ?

0

Sorry, I am late. You should just make sure that it doesn't kill Players

part.Touched:Connect(function(p)
    if p.Parent.FindFirstChild == ("Humanoid") then 

        local plr = workspace[p.Parent.Name]

        wait(0.5)
            plr.humanoid.Health = plr.humanoid.Health - 20
        wait(0.1)
        part:Destroy()


    end

end)

this isn't tested but try it atleast.

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