Accurately Detect if Player Steps On/Off Button Using Touched/TouchEnded

Hello,

I have been trying to make a button go down when a player steps on it, and have it go back up when they are not touching it anymore. However, this is quite challenging as the Touched/TouchEnded events are not very accurate (when the player moves around on the button, both Touched and TouchEnded events fire.

I have tried using debounces and delays, but none work so far. Is there anything that can accurately fire once when the player steps on the button, and fire again when they step off?

Thank you

1 Like

Yes, you should detect the magnitude manually, or use .Touched on the client.

1 Like
local buttonActivated = false -- currently OFF

local function turnOnButton()
    if buttonActivated == false then -- prevents spam, and will turn ON only once, even if you leave and touch it again ( you must define how to disable this variable
        buttonActivated = true
     
        print("Button has been turned on forever")
    end
end

-- Event to detect player touch
button.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then -- make sure to place some debounce here and turn it ON so it will return forever and will never activate again until you create another function that disables it
        ActivateButton() -- will activate once
    end
end)

I was using similar code, but my main problem was when the player stepped off. The TouchEnded is trigger even though the player is not fully off the button. I assume Region3 or Magnitude should be an additional check for the player stepping off?

creating a certain type of hitbox and using region3 would be a good idea and accurate to check if anything is inside, you will need to create an artifical box on the top of the button to check for this, prevents exploiters too (activating from far)

1 Like

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