Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Part touched connect function (???)

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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)

0
Its worked but its killing instantly, i want do to once 20 when u Activated the tool aSpecialGuy 18 — 4y
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 ? aSpecialGuy 18 — 4y
0
Sorry, I am late. You should just make sure that it doesn't kill Players TheJellyNinja_XD13 62 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
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.

Answer this question