How do I receive gui signals to my localscript?

Hello there.

To specify, what I want to achieve is that I want to make controls that will be utilized in my game, for platform support on mobile.

I tried using the bool value used on contextactionservice:bindaction() to make the control on mobile, but it seems as if there’s a limit for 7 buttons max you can make with that bool value, it seems to be a bug that is still not fixed, I mean sure, if there’s less buttons on screen there won’t be a problem, but, well…

I use 12 buttons total on screen, so in turn I can’t use the contextactionservice to set my buttons, instead I have to go for GUI buttons on screen and basically reinvent the wheel because there’s no big support for it.

Here’s another major problem. I want the localscript on starterplayerscripts to receive the signals when the buttons are being pressed, and I don’t want to write spaghetti code, so I need the ScreenGui that is inside the StarterGui to give the signals when being pressed to the localscript on starterplayerscripts, and I don’t know how to achieve this.

I feel like this is just a lot more simple and I’m going around circles simply because I’m tired asf and listening to everywhere at the end of time wasn’t a good idea (specially when I have ONE OF THEIR ######## SONGS STUCK ON MY HEAD), so yeah, maybe I just need to go to sleep and this will just get fixed with time, but that’s why I’m posting this at night, to leave something for the night.

Now, for the future me, please tell me what the fu# wihere tou doing at this hour god fu$$hfin thamit im tired pleas help me.zzzzzzzzzzzz

Sometimes you have to realise that you need a break.

Anyhoo. Nothing wrong with using a script from StartPlayer to listen to the UI. The following assumes that all your buttons are in one frame

-- set some variables for the players UI & the frame itself
local player = game.Players:WaitforChild("LocalPlayer")
local ui = player.PlayerGui.ScreenGui
local frame = ui.<yourframe>

-- iterate through the frame and connect an event to each button
for i,v in pairs(frame:GetDescendants()) do		-- loop through the items in the frame
	if (v:IsA("ImageButton")) then			-- if its an image button then...
		v.Activated:Connect(function()		-- Connects an event to each 
			-print(v.Text, " button clicked")		-- Yay, we detected a button click
			-- Add your event stuff here...
1 Like

How would I do to give a precise function to each button? I have specific actions for each button but I can’t give them one to each if not by putting if elseif elseif elseif elseif elseif and so on.

on --add your event stuff here, I don’t know how to give each a specific function, one needs to change the positionx value to 0.5 when it’s pressed and then to 0 when it’s not being pressed, the other one needs to change the rotation of a piece that is in the game later on. Some are activated just when pressed, the others need the signal when being pressed and another one when they stop pressing.

I guess you don’t want to do a bunch of elseif statements looking at some of you other posts. In which case refer back to Autterfly’s reply to you which said:

… storing the code in functions. The functions would be put into a table by some key which your if might’ve used to compare. In this case, you can rewrite it to just do branches_table[key](any_additional_info)

You might have to ask him for a code example.

1 Like

It’s not really that I have an inner hate to elseifs, rather it’s more that I would have to put 12 elseif chains in one script which is not ideal for coding practices. The person you quoted said that too, that

Which I totally agree, and which I would have to avoid.

I will see what this person was saying either way.