#How to run code when a button is pressed (plus how to allow a button to be pressed)

1 messages · Page 1 of 1 (latest)

orchid bison
#

I have three buttons that the user can switch between with the arrow keys. I want it so that when the player presses "z", then, depending on the button selected, code will be run. Can someone give me a hand with this?

#

here is my code so far:

#

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")

local HideButtons = 0

local selectionIndex = 1
local options = {"FIGHT", "ACT", "ITEM"}

local optionImages = {
    FIGHT = "http://www.roblox.com/asset/?id=133771545169390", -- Replace with actual asset IDs
    ACT = "http://www.roblox.com/asset/?id=71502386035349",
    ITEM = "http://www.roblox.com/asset/?id=72093908802478"
}

local menuFrame = Instance.new("Frame")
menuFrame.Position = UDim2.new(0.5, -200, 0.75, -50)
menuFrame.Size = UDim2.new(0, 200, 0, 50)
menuFrame.BackgroundTransparency = 1
menuFrame.Parent = screenGui
menuFrame.ZIndex = 2

local buttons = {}
for i, option in ipairs(options) do
    local btn = Instance.new("ImageLabel")
    btn.BorderSizePixel = 5
    btn.BorderColor3 = Color3.fromRGB(100, 0, 3)
    btn.BackgroundColor3 = Color3.fromRGB(158, 0, 3)
    btn.Size = UDim2.new(0, 50, 0, 50)
    btn.Position = UDim2.new((i-1) * 0.74, 0, 0, 0)
    btn.Image = optionImages[option]
    btn.Parent = menuFrame
    table.insert(buttons, btn)
end```
#
    for i, btn in ipairs(buttons) do

        if i == selectionIndex then
            if HideButtons == 0 then
                btn.BackgroundTransparency = 0
                btn.ImageTransparency = 0
                btn.BorderSizePixel = 7.5
                btn.Active = 1
            else
                btn.BackgroundTransparency = 1
                btn.ImageTransparency = 1
                btn.BorderSizePixel = 0
                btn.Active = 0
            end
            btn.Size = UDim2.new(0, 150, 0, 150)
            btn.Position = UDim2.new(btn.Position.X.Scale, -25, btn.Position.Y.Scale, -25)

        else
            if HideButtons == 0 then
                btn.BackgroundTransparency = 0
                btn.ImageTransparency = 0.5
                btn.BorderSizePixel = 5
                btn.Active = 1
            else
                btn.BackgroundTransparency = 1
                btn.ImageTransparency = 1
                btn.BorderSizePixel = 0
                btn.Active = 0
            end
            btn.Size = UDim2.new(0, 100, 0, 100)
            btn.Position = UDim2.new(btn.Position.X.Scale, 0, btn.Position.Y.Scale, 0)

        end
    end
end

local function animateIcons()
    local direction = 1
    local amplitude = 0.3
    local baseFrequency = 1.5
    local time = 0
    while true do
        for i, btn in ipairs(buttons) do
            local offset = amplitude * math.sin(time * baseFrequency)
            local dynamicFrequency = baseFrequency * (1 - math.abs(offset) / amplitude)
            btn.Position = UDim2.new(btn.Position.X.Scale, btn.Position.X.Offset, 0.75 + offset, btn.Position.Y.Offset)
        end
        time = time + 0.1
        task.wait(0.2)
    end
end```
#
UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Right then
        selectionIndex = (selectionIndex % #options) + 1
        updateSelection()
    elseif input.KeyCode == Enum.KeyCode.Left then
        selectionIndex = (selectionIndex - 2) % #options + 1
        updateSelection()
    end
end)

updateSelection()
animateIcons()```
#

(these are all one script, I just had to break it apart)

quick mantle
orchid bison
#

I've never really worked with buttons before

#

let alone with the majority of code; I'm really new to the Roblox Studio coding language

quick mantle
#

Oh you need a Gui button?

orchid bison
#

oh- do I..?

#

what do I have right now? I thought I had them as buttons-

#

or- I'm not looking for clickable buttons, but buttons similar to, like, UNDERTALE's gui buttons during fights, where you scroll through them and press z when you're over the one you want to press

quick mantle
fleet islandBOT
#

studio** You are now Level 1! **studio