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

How do I check if a player is not touching a brick?

Does anyone know how to check if a player is not touching a brick? I'm not sure how to do this.

1 answer

You have to use Touch and TouchEnded (you keep track of which players are touching the specified brick; if a player is not in that list, they are not touching it), unless you're willing to test for player proximity (ie test to see if a player is within some range of the target object). In testing, I discovered that TouchEnded doesn't always fire after Touched, nor will it always fire properly for HumanoidRootPart. Here's a "prototype" you can test out on your own:

parts = {}
workspace.SpawnLocation.Touched:connect(function(p) parts[p] = true end)
workspace.SpawnLocation.TouchEnded:connect(function(p) wait() parts[p] = nil end)
while wait(1) do
	print("----")
	for k, _ in pairs(parts) do
		print(k)
	end
end

You'll notice that if you touch the SpawnLocation and then stop touching it, HumanoidRootPart will usually still be being printed out.

The wait() in the 3rd line solves the problem where TouchEnded fires before Touched does. A proper function should keep track for each player and ignore HumanoidRootPart. I don't know if TouchEnded fires when a player leaves the game, so that should be considered also.

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