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