How to create a floor button

Hi there !
I would like to code some sort of button on the floor for a puzzle game.
It would be similar as the ones in portal:

I dont want to use the touched event because its wierd and unpredictable.
Any idea on how to code this?

1 Like

You could use a region3 in the area of the button that detects when a player enters it

I believe Region3 has been superseded by something, and may now be considered deprecated. I remember reading something about this, correct me if I’m wrong.

Or you can use .magnitude, found in many FindNearrestTorso scripts, but strip the Y Value out of the check, unless you have multiple floors with Buttons on top of each other… ((Or set the actual /button invisable, and have it at Torso hieght (Or check against Player’s leg, instead of at his Torso))

I believe this is what is after Region3: OverlapParams

3 Likes

I’ll try to use that, thanks!!

1 Like

Can you post how you set this up? I’m curious how to use getpartsinpart in a way that is similar to touched. Do you have to just use an endless loop to keep checking or is there a way to set up a triggered connection?

I am trying to figure this out right now.
If you want to help me, you are welcome !

Well, I have done this with a looped interval query, but that does not seem like a desirable way to do it. It doesn’t seem efficient to keep checking something whether or not it has changed.

The touched event is fine.

local players = game:GetService("Players")
local button = workspace.Button
local debounce = false

button.Touched:Connect(function(hit)
	if debounce then
		debounce = false
		task.wait("5")
		return
	end
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		debounce = true
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print(player.Name, " touched the button!")
	end
end)

yeah but it fires many times when only pressed once and does not tell me when it stops being pressed…

It works fine till it doesn’t. What if your player’s tool or a part within an accessory they’re wearing touches the part first, in which case it won’t find the humanoid but will trigger the debounce and your button is now dead for x seconds.

This version won’t, I’ve added a debounce which prevents the function from being executed too often (in this case the timeout is set to 5 seconds).

Right and you get false positives and a dead button for an interval.

You can perform a check on hit itself, to make sure it’s named “Left Leg” or some other body part of the character.

well it does…
I tested it and it fired 7 times

So i would like to know how to use
:GetPartsInPart(PartInstance part, OverlapParams overlapParams)
and i cant find how ;-;

There are many ways to make it work better, but its still not entirely predictable in my experience. One way is to make a trigger part and wait for only the humanoidrootpart to touch it(throw out all other triggers), then debounce. But then you have the problem of multiple players and having to account for more than one player needing to activate the button at the same time(or close to it). You could make a table of player who touched it and debounce per player, etc and etc. But there has to be a better way, no?

This forum post should have enough information:

1 Like

It returns a table of parts that are within the part. But then you’ll have the problem of how often you check to see if a player part is in there.

You don’t want a game full of buttons polling in infinite loops looking for players.