Starting Tool Choices GUI Not Functioning Properly

Hello! So I’m trying to make a script where, before people play a round, they can choose their starting tools.

I’ve scripted everything, and made a bunch of progress, but after I finished, it turned out not to work.

Here’s a screenshot of my StarterGui:
image

Next, here’s the LocalScript with all of the values:

--values--
local choiceOne = script.ChoiceOne.Value
local choiceTwo = script.ChoiceTwo.Value
local choiceThree = script.ChoiceThree.Value
--table of buttons--
local buttons = {
	One = script.Parent.ToolOneButton.TextButton,
	Two = script.Parent.ToolTwoButton.TextButton,
	Three = script.Parent.ToolThreeButton.TextButton,
	Four = script.Parent.ToolFourButton.TextButton,
	Five = script.Parent.ToolFiveButton.TextButton
}
--tables of frames--
local frames = {
	One = script.Parent.ToolOne,
	Two = script.Parent.ToolTwo,
	Three = script.Parent.ToolThree,
	Four = script.Parent.ToolFour,
	Five = script.Parent.ToolFive
}
--functions--
buttons.One.MouseButton1Click:Connect(function()
	if choiceOne == nil then
		choiceOne = "Tool One"
		script.Parent.One.Visible = true
	elseif choiceOne ~= nil and choiceTwo == nil then
		choiceTwo = "Tool One"
		script.Parent.One.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree == nil then
		choiceThree = "Tool One"
		script.Parent.One.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree ~= nil then
		script.Parent.TextLabel.Visible = true
		wait(3)
		script.Parent.TextLabel.Visible = false
	end
end)

buttons.Two.MouseButton1Click:Connect(function()
	if choiceOne == nil then
		choiceOne = "Tool Two"
		script.Parent.Two.Visible = true
	elseif choiceOne ~= nil and choiceTwo == nil then
		choiceTwo = "Tool Two"
		script.Parent.Two.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree == nil then
		choiceTwo = "Tool Two"
		script.Parent.Two.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree ~= nil then
		script.Parent.TextLabel.Visible = true
		wait(3)
		script.Parent.TextLabel.Visible = false
	end
end)

buttons.Three.MouseButton1Click:Connect(function()
	if choiceOne == nil then
		choiceOne = "Tool Three"
		script.Parent.Three.Visible = true
	elseif choiceOne ~= nil and choiceTwo == nil then
		choiceTwo = "Tool Three"
		script.Parent.Three.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree == nil then
		choiceTwo = "Tool Three"
		script.Parent.Three.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree ~= nil then
		script.Parent.TextLabel.Visible = true
		wait(3)
		script.Parent.TextLabel.Visible = false
	end
end)

buttons.Four.MouseButton1Click:Connect(function()
	if choiceOne == nil then
		choiceOne = "Tool Four"
		script.Parent.Four.Visible = true
	elseif choiceOne ~= nil and choiceTwo == nil then
		choiceTwo = "Tool Four"
		script.Parent.Four.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree == nil then
		choiceTwo = "Tool Four"
		script.Parent.Four.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree ~= nil then
		script.Parent.TextLabel.Visible = true
		wait(3)
		script.Parent.TextLabel.Visible = false
	end
end)

buttons.Five.MouseButton1Click:Connect(function()
	if choiceOne == nil then
		choiceOne = "Tool Five"
		script.Parent.Five.Visible = true
	elseif choiceOne ~= nil and choiceTwo == nil then
		choiceTwo = "Tool Five"
		script.Parent.Five.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree == nil then
		choiceTwo = "Tool Five"
		script.Parent.Five.Visible = true
	elseif choiceOne ~= nil and choiceTwo ~= nil and choiceThree ~= nil then
		script.Parent.TextLabel.Visible = true
		wait(3)
		script.Parent.TextLabel.Visible = false
	end
end)

I also have a script in all of the NumberedImageLabels>TextButtons, edited for each button specifically:

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.Parent.LocalScript.ChoiceOne.Value == "Tool One" then
		script.Parent.Parent.Parent.LocalScript.ChoiceOne.Value = nil
		script.Parent.Visible = false
	elseif script.Parent.Parent.Parent.LocalScript.ChoiceTwo.Value == "Tool One" then
		script.Parent.Parent.Parent.LocalScript.ChoiceTwo.Value = nil
		script.Parent.Visible = false
	elseif script.Parent.Parent.Parent.LocalScript.ChoiceThree.Value == "Tool One" then
		script.Parent.Parent.Parent.LocalScript.ChoiceThree.Value = nil
		script.Parent.Visible = false
	end
end)

Now, I have a video clip of it not working:

3 Likes

The error may be in the beginning where the local script says:

local buttons = {
One = script.Parent.ToolOneButton.TextButton,
Two = script.Parent.ToolTwoButton.TextButton,
Three = script.Parent.ToolThreeButton.TextButton,
Four = script.Parent.ToolFourButton.TextButton,
Five = script.Parent.ToolFiveButton.TextButton
}

But when you look in the Explorer, there is no object called ToolOneButton or ToolTwoButton and so on. I think you meant to type just One, Two, Three and so on. They are the objects with the TextButtons you are referencing.

Hope that solves your issue :smile:

1 Like

You should really use loops instead of making 5 different Mouse click detectors.

for i = 1,#buttons do
       buttons[i].MouseButton1ClickConnect(function()
                --Put your code here.
        end) 
end

You are repeating the same thing in a number sequence so I recommend that you configure more of your code to just use number values so it is more easy to set up.

Make sure you check if there is no syntax errors or make sure you are not setting the wrong child in the buttons table.

Hey there, if you expand the image, they’re at the bottom.

How could I find the button that was clicked? :thinking:

Like I said you should use number values instead of string numbers so you can find the right one just using the “i” variable. The i variable is the amount of times it looped. Loop it by the number of buttons and set for each Tool.

So just for clarification, the i variable can be used to see what button was clicked?

Exactly. Let me give you an example…

local buttons = {
     1 = script.Parent.ToolOneButton.TextButton,
     2 = script.Parent.ToolTwoButton.TextButton,
     3 = script.Parent.ToolThreeButton.TextButton,
     4 = script.Parent.ToolFourButton.TextButton,
     5 = script.Parent.ToolFiveButton.TextButton
}
for i = 1,#buttons do
       buttons[i].MouseButton1ClickConnect(function()
                local Button = buttons[i]
        end) 
end

Still not working? (I used IntValues, not NumberValues, though. I tried NumberValues but the same thign happened.)

Script:

--values--
local choiceOne = script.ChoiceOne.Value
local choiceTwo = script.ChoiceTwo.Value
local choiceThree = script.ChoiceThree.Value
--table of buttons--
local buttons = {
	One = script.Parent.ToolOneButton.TextButton,
	Two = script.Parent.ToolTwoButton.TextButton,
	Three = script.Parent.ToolThreeButton.TextButton,
	Four = script.Parent.ToolFourButton.TextButton,
	Five = script.Parent.ToolFiveButton.TextButton
}
--tables of frames--
local frames = {
	One = script.Parent.ToolOne,
	Two = script.Parent.ToolTwo,
	Three = script.Parent.ToolThree,
	Four = script.Parent.ToolFour,
	Five = script.Parent.ToolFive
}
--functions--
for i = 1, #buttons do
	buttons[i].MouseButton1Click:connect(function()
		local ButtonClicked = buttons[i]
		if ButtonClicked == buttons.One then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Two then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Three then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Four then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Five then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		end
	end)
end

Be sure to check if you got any errors. You can do this by test playing and clicking f9 or just simply test play and have the output open.

No errors. Just my prints, and Ro-Defender.

It’s not working because you’re trying to count from 1 to 0 with a positive increment. Let me explain.

The length operator # gets the length of the array part of a table. Your buttons table has no numerical indexes, so it has no array part.

So #buttons evaluated to 0, which then looks more like

for i = 1, 0[, 1] do -- [, 1] just there to show that the increment is optional, but if not provided defaults to 1

end

That aside, if you number them 1, 2, 3 etc instead of one, two, three, etc it will be a lot easier to access the items.

For example

-- if you renamed the items to Tool1Button, Tool2, etc this will work

local frame = script.Parent -- D.R.Y.*, and idk if this is a frame, do name variable appropriately if it isn't
local formats = {"Tool%d", "Tool%dButton"}
local buttons = {}
for i = 1, 5 do
    table.insert(buttons, frame[formats[2]:format(i)])
end

local frames = {}

for i = 1, 5 do
    table.insert(frames, frame[formats[1]:format(i)])
end

If you don’t know how format works, and wha the %d is for, I’ll give a quick explanation:

format basically formats a string based on format specifiers in the string, and returns the formatted string

%d is one of those format specifiers, for digits

So string.format("Tool%d", 5) == "Tool5" for example.

* Don’t Repeat Yourself
1 Like

Script:

--values--
local choiceOne = script.ChoiceOne.Value
local choiceTwo = script.ChoiceTwo.Value
local choiceThree = script.ChoiceThree.Value
--frame variable--
local frame = script.Parent
--formats--
local formats = {"Tool%d", "Tool%dButton"}
--table of buttons--
local buttons = {}
--tables of frames--
local frames = {}
--functions--
for i = 1, 5 do
	table.insert(buttons, frame[formats[2]:format(i)])
	table.insert(frames, frame[formats[1]:format(i)])
	buttons[i].MouseButton1Click:connect(function()
		local ButtonClicked = buttons[i]
		if ButtonClicked == buttons.One then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Two then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Three then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Four then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		elseif ButtonClicked == buttons.Five then
			if script.ChoiceOne.Value == 0 then
				script.ChoiceOne.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value == 0 then
				script.ChoiceTwo.Value = i
			elseif script.ChoiceOne.Value ~= 0 and script.ChoiceTwo.Value ~= 0 and script.Parent.ChoiceThree.Value == 0 then
				script.ChoiceThree.Value = i
			else
				script.Parent.OptionsFull.Visible = true
				wait(3)
				script.Parent.OptionsFull.Visible = false
			end
		end
	end)
end

The output says:

Loading Cutscene Editor... (x2)
Cutscene Editor Loaded (x2)
Ro-Defender has removed a total of 0 viruses and junk items from your games! (x2)
  21:58:49.880 - MouseButton1Click is not a valid member of ImageLabel
21:58:49.881 - Stack Begin
21:58:49.882 - Script 'Players.MaximussDev.PlayerGui.ToolsGUI.Frame.LocalScript', Line 17
21:58:49.883 - Stack End
21:58:59.545 - Disconnect from 127.0.0.1|52984

The error explains itself, could you show me the new hierarchy of script.Parent? I’m confused right now, you probably put image labels instead of image buttons?

The script.Parent is currently the Frame.

Just before connecting the event, check if it’s a gui button:

if buttons[i]:IsA("GuiButton") then
    local currentButton = buttons[i]

    currentButton.Activated:Connect(function()
        -- stuff 
    end)
end

Also buttons.One, … buttons.Five will all result in nil since the table has no string indexes. You will want to use buttons[1], … buttons[5] instead.

And you can avoid some repetition if you used these

local choiceOne = script.ChoiceOne
local choiceTwo = script.ChoiceTwo
local choiceThree = script.ChoiceThree

OK, but it’s trying to contact a MouseButton1Click:connect() from a frame? I’m just not sure how to get it…

Is there a way to do this?

(I added an else line, which just calls the error() event: error("Not supported"))

Nevermind, I figured it out! Thanks! :slight_smile:

2 Likes

Lol you can make it where if they click which tool they want it destroys the whole ui after(so they can’t choose multiple)

no, they get 3 choices. I got it fixed