Help with detecting if player is touching part

I want to achieve a working script that can print “Player is touching part” every second if a player is standing still on a part. If the player steps off the part, it stops printing that. How can I do this?

You can create a variable (within the script) or BoolValue to determine when a player is currently on a part or not.

Example (let’s use a variable):

local part -- locate your part
local onPart = false -- variable

part.Touched:Connect(function(hitPart)
	if hitPart.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)

		if player then
			onPart = true
		end
	end
end)

part.TouchEnded:Connect(function(hitPart)
	if hitPart.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hitPart.Parent)

		if player then
			onPart = false
		end
	end
end)

Since you want to print a message every second, you could either use a ‘While’ loop or use RunService

coroutine.wrap(function() -- this is to prevent the rest of the script from yieldinh
	while onPart do -- while 'onPart' is true
		print("Player is touching part")
		task.wait(1)
	end
end)()
2 Likes

thanks, it works
(insert filler text here)

1 Like

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