Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

item switching gui?

Asked by
Paldi 109
8 years ago

I made a GUI to switch between somethings and i think the way i made it is too long to do because i need to keep adding "else" for each items and i got a lot, can anyone help me figure this out? like a faster way to do it, instead of the need to add each items

function OnClicked()
   local items = script.Parent.Parent.ItemName
    if items.Value == "None" then
       items.Value = "item1" 
    elseif items.Value == "item1" then
         player.Character:findFirstChild(items.Value):remove()
         items.Value = "item2"
    elseif items.Value == "item2" then
         player.Character:findFirstChild(items.Value):remove()
           items.Value = "item3"
    elseif items.Value == "item3" then
         player.Character:findFirstChild(items.Value):remove()
           items.Value = "item4"
    elseif items.Value == "item4" then
         player.Character:findFirstChild(items.Value):remove()
           items.Value = "item1"

--etc

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

If they're all named "item" with some number after it, and there's a set number of items, you can do some string manipulation and math:

local num=10 -- highest item number
function OnClicked()
    local items=script.Parent.Parent.ItemName
    if items.Value=="None"then
        items.Value="item1"
    else
        local n=tonumber(items.Value:match("%d+"))
        if n==num then
            items.Value="None"
        else
            items.Value="item"..(n+1)
        end
    end
end
--etc
0
thats interresting i think i can make this work with it, thanks Paldi 109 — 8y
Ad

Answer this question