See how long a player touches a part?

So I’m trying to make a script where if you stand on a part for more than 3 seconds, it kills you. I obviously know about touched events and access the player but I’m not sure how to return how long the player was touching the part, anyone know how?

I actually made script for this not so long ago, you can check it out :smiley:

local part = script.Parent 
local killTime = 3 
local debounce = false 

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not debounce then
		debounce = true
		local startTime = tick()

		repeat
			wait()
			print(part:GetTouchingParts())
		until tick() - startTime >= killTime or not table.find(part:GetTouchingParts(),hit)

		if table.find(part:GetTouchingParts(),hit) then
			hit.Parent.Humanoid:TakeDamage(100)
		end

		debounce = false
	end
end)

Altough there is probably a more effective way by using raycast.

if otherPart:FindFirstChild("Humanoid") then
	if debounce = true then
		debounce = false
		otherPart.Humanoid.Health = otherPart.Humanoid.Health - 35
	end
	wait(1) -- just add a delay and on the 3rd time this script runs it will kill the player
	debounce = true
else
-- no one is touching the part
end

Or something like that. Just use a wait().

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.