Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

item switching gui?

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

✓ Accepted answer

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

1 comment

0

thats interresting i think i can make this work with it, thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.