How can I make a "Choose weapon" system like with buttons "<-" and "->"?

I want to make a choose weapon system like on the images:

First Image. The buttons to understand more.

Second Image, I want it to go from the first gun in example, AK47 all the way down to Uzi (not shown)
if it would pass uzi, it would go again to AK47 etc.

obraz_2024-04-12_172216556

Heres a video showing that how it changes the guns. It is already done, but now how can I make it change to the next on the gun list?

Hope someone can help.

2 Likes

You can simply use an index and then when the player clicks the left or right button, it would add or subtract the index and then you can use;

script.Selected:GetChildren()[selectionIndex]

to set the gun to active.

1 Like

I quite don’t understand indexing. I am supposed to make a local with the selection index and then what? Sorry I just don’t understand this…

Hello! Here’s a simple example script for an interface that allows you to select and scroll through weapons by pressing arrow buttons:

local gui = script.Parent
local weaponList = {
    "Pistol",
    "Shotgun",
    "Assault Rifle",
    "Sniper Rifle"
}
local currentWeaponIndex = 1

-- Function to update displayed weapon
local function updateWeaponDisplay()
    gui.WeaponLabel.Text = weaponList[currentWeaponIndex]
end

-- Initial update of weapon display
updateWeaponDisplay()

-- Function to scroll to the next weapon
local function nextWeapon()
    currentWeaponIndex = currentWeaponIndex % #weaponList + 1
    updateWeaponDisplay()
end

-- Function to scroll to the previous weapon
local function previousWeapon()
    currentWeaponIndex = (currentWeaponIndex - 2 + #weaponList) % #weaponList + 1
    updateWeaponDisplay()
end

-- Event handlers for arrow buttons
gui.LeftButton.MouseButton1Click:Connect(previousWeapon)
gui.RightButton.MouseButton1Click:Connect(nextWeapon)

This script assumes you have a GUI with two arrow buttons (e.g., LeftButton and RightButton) and a text element (e.g., WeaponLabel) where the currently selected weapon will be displayed.

When you click the left arrow button (LeftButton), the previous weapon in the list will be selected, and when you click the right arrow button (RightButton), the next weapon in the list will be selected.

1 Like

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