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

How would I make a part act as a barrier that kills you if you touch it for 5 seconds?

Basically, what I've been trying to do, is make it so if a player touches this part or is in this part for 5 seconds, they will be killed. The same way a game will kill you if you "are out of the battlefield etc." I've tried to replicate that with this code:

Part = script.Parent

   Part.Touched:Connect(function(hit)

	if hit.Parent:FindFirstChild("Humanoid") ~= nil then

		wait(5)

		Part.TouchEnded:Connect(function(Away)

			if Away then

				hit.Parent.Humanoid.Health = 100

		else 

			hit.Parent.Humanoid.Health =  0

			end

		end)

	end

end)

I would love to know what I'm doing wrong, along with any other tips or advice. Thanks.

1 answer

Part = script.Parent
Duration = 5

Part.Touched:connect(function(hit)
	if hit then 
		if game:GetService("Players"):FindFirstChild(hit.Parent.Name) then 
			player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
			spawn(function()
				wait(Duration)
				Part.TouchEnded:connect(function(part)
					if part and (part.Parent.Name == player.Name) then 
						player.Character:BreakJoints()
					end
				end)
			end)
		end
	end
end)

2 comments

0

Not sure if this would work but worth a try

0

It didn't work, however the results were definitely interesting. What would happen is, every time I would go fully into the part, I could stay there as long as I wanted but the second I stepped out I would instantly die. And if I was on the outside and just touched it for a second, I would live but the next time I touched it I would instantly die.

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