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

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

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?

3 comments

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?

0

dude u cant actually touch a forcefield

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

3 answers

✓ Accepted answer

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.

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)
-- 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)

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