Making A Button

I am trying to make a button like this. When you step on it, something happens. However, I can’t seem to figure out how to do this. I don’t know what they are using to do this, I don’t see how they would do it with a touch event, how would it know if you are off the button? If you can help, please reply!
image

6 Likes

A pretty simple script, place a server script inside of a part.

local Debounce = false -- Acts like your cooldown.
script.Parent.Touched:Connect(function(OnHit)
	if not Debounce then
		Debounce = true
		--- Your Code Here
		task.wait(1) -- How long the cooldown last.
		Debounce = false
	end
end)
2 Likes
  • Use the Touched event of the part to detect when a player steps on it.

This is a simple script you can use

local button = script.Parent -- Assuming the script is placed inside the button part

local function onTouched(otherPart)
    local character = otherPart.Parent -- The model or object the otherPart belongs to
    local player = game.Players:GetPlayerFromCharacter(character) -- Get the player associated with the character
    
    if player then
        -- Trigger your desired action here, e.g., activating a door, changing a variable, etc.
        print(player.Name .. " stepped on the button!")
    end
end

button.Touched:Connect(onTouched)

Remember to replace the print statement with the action you want the button to perform

3 Likes

Yes, obviously you could use a touch event. How would you detect when they step off?

2 Likes

I don’t need a debounce, I need to know when they step off and on the part.

1 Like

Oh, my bad. I think you should use script.Parent.TouchEnded:Connect(function()), hopefully this will help.

-- Function to handle when a player leaves the part
local function onTouchEnded(otherPart)
    local character = otherPart.Parent
    local player = game.Players:GetPlayerFromCharacter(character)

    if player then
        print(player.Name .. " left the part.")
        -- You can put your code here to do something when a player leaves the part
    end
end

part.TouchEnded:Connect(onTouchEnded)
3 Likes

You can use a combination of the Touched event and the Humanoid:MoveTo()

local button = script.Parent -- Assuming the script is placed inside the button part

local playersOnButton = {} -- Keeps track of players currently on the button

local function onTouched(otherPart)
    local character = otherPart.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    
    if player then
        playersOnButton[player] = true
        print(player.Name .. " stepped on the button!")
        
        -- You can perform any actions related to stepping on the button here
    end
end

local function onUntouched(otherPart)
    local character = otherPart.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    
    if player then
        playersOnButton[player] = nil
        print(player.Name .. " stepped off the button!")
        
        -- You can perform any actions related to stepping off the button here
    end
end

button.Touched:Connect(onTouched)
button.TouchEnded:Connect(onUntouched)

After testing, I realized this is really inaccurate, it flashed a lot and touchended doesnt always fire, does anyone have a more reliable method?

you could try spamming getTouchingParts?

Thats because when you are moving on top of the button, you are essentially “not touching” the button anymore. You could have a invisible noncollideable boundarypart that detects the touch instead.

add a big cylinder which the button only detects

Is it a roblox beta feature thats messing it up or is roblox just making the script less responsive?
I tried this out and it worked fine as usual.

1 Like

Nah. The part is flashing due to one of the Robloxian’s legs lifting off the ground.

Add a larger hitbox that detects your character. Something like @Angermode said.

1 Like

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