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

how to make a kill a player with part.Touched?

Asked by 5 years ago

I am trying to make a custom force field script. where when someone touches the force field they die. But, when someone touches it, it never kills them.

here is what I have

force1.Touched:Connect(function(hit)
                if hit.Parent.Humanoid or hit.Parent.Parent.Humanoid then
                    if hit.Parent.Name ~= plr.Name then
                        hit:Destroy()
                    end
                end
            end)
            force2.Touched:Connect(function(hit)
                if hit.Parent.Humanoid or hit.Parent.Parent.Humanoid then
                    if hit.Parent.Name ~= plr.Name then
                        hit:Destroy()
                    end
                end
            end)
            force3.Touched:Connect(function(hit)
                if hit.Parent.Humanoid or hit.Parent.Parent.Humanoid then
                    if hit.Parent.Name ~= plr.Name then
                        hit:Destroy()
                    end
                end
            end)

it does not give an error but it doesnt work can someone help please?

0
What is the "plr" and why do you make an 'if statement' checking if it is not equal to the name of the person who touched it? Pojoto 329 — 5y
0
dude u cant actually touch a forcefield TheluaBanana 946 — 5y
0
its not an actual forcefield object its a part welded ti the players torso that is a circle that will act as a forcefield imnotacoder 13 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

Try this.

function ontouched(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") or hit.Parent.Parent:FindFirstChildOfClass("Humanoid") then
        if hit:IsA("Part") then
            hit:Destroy()
        end
    end
end

force1.Touched:Connect(ontouched)
force2.Touched:Connect(ontouched)
force3.Touched:Connect(ontouched)

I replaced some if statements, but this should kill the player. What was happening was the if statement was always returning false, so the part was never destroyed.

Ad
Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago

It looks like your over complicating this :p

Let's say you have 3 force fields named force1, force2, and force3

To make it so that when a character touches them they die do this

force1.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")~=nil then
        hit.Parent.Humanoid.Health=0
    end
end)
force2.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")~=nil then
        hit.Parent.Humanoid.Health=0
    end
end)
force3.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")~=nil then
        hit.Parent.Humanoid.Health=0
    end
end)
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
-- Put Scripts into the ForceFields and put this:
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        if hit.Parent.Humanoid.Health > 0 then
            hit.Parent.Humanoid:TakeDamage(hit.Parent.Humanoid.MaxHealth)
        end
    end
end)

Answer this question