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

How to make script work for all ImageButtons inside of a GUI?

How would making this work for ALL ImageButtons in a GUI work? I'd like to have one main script instead of having to copy and paste it inside of all the ImageButtons. It clones a ParticleEmitter and Sound from inside of the Script and removes them after some time when the ImageButton is clicked.

Not too sure how I'd go about laying it out or if I'm doing it right.

for i,c in pairs(GetChildren()) do
	if c:IsA'ImageButton' then
	c.MouseButton1Click:connect(function()
function onClicked()
	local particle = script.ParticleEmitter:Clone()
	game:GetService("Debris"):AddItem(particle, .35)
	particle.Parent=Character.Torso
	particle.Enabled = true
	
	wait(.35)
	local sound = script.TransformSound:Clone()
	game:GetService("Debris"):AddItem(sound, 3)
	sound.Parent = Character
	sound:Play()
	wait(3)

end 
script.Parent.MouseButton1Click:connect(onClicked)

1 answer

✓ Accepted answer

This script should work You binded same function two times I mean You did something like this OnClicked > OnClicked > Function This should work

local Character = game.Players.LocalPlayer.Character
function check(holder)
for i,c in pairs(holder:GetChildren()) do
    if c:IsA'ImageButton' then
        c.MouseButton1Click:connect(function()
             local particle = script.ParticleEmitter:Clone()
             game:GetService("Debris"):AddItem(particle, .35)
             particle.Parent=Character.Torso
             particle.Enabled = true

             wait(.35)
             local sound = script.TransformSound:Clone()
             game:GetService("Debris"):AddItem(sound, 3)
             sound.Parent = Character
             sound:Play()
             wait(3)
        end)
    end
end 
end
for _, i in pairs(script.Parent:GetChildren()) do
 if string.upper(string.sub(i.Name, 1, 1)) == "C" then
  check(i)
 end
end
for _, i in pairs(script.Parent.Parent:GetChildren()) do
 if string.upper(string.sub(i.Name, 1, 1)) == "C" then
  check(i)
 end
end

Updated script This should be placed in LocalScript

18 comments

0

I have the hierarchy like this: AlbedoGUI - Main - C1, I'd like for it to check through any page beginning with C. I tried this: local holder = script.Parent['C'] but not sure if it's correct. Also, it says "Unknown global Character"

0

The GUI is inside of the ReplicatedStorage, and is taken from there when a tool is used.

0

At the start add local Character = game.Players.LocalPlayer.Character

0

also script might be LocalScript so it will work (about this ['C'] You did this alright)

0

Updated script. It should work now. I didnt know that there isnt definied Character variable. Sorry

0

defined*

0

It says that C is not a valid member of Frame

0

are You sure that There is also C thing in Parent of script ( are u sure that there is a thing named C in the same place where script is held)?

0

I have many Frames (pages of GUI) named like C1, C2. The script is inside of a Frame called Main inside of AlbedoGUI.

0

and is C frame in Main frame too?

0

if no then set location to script.Parent.Parent["FULL NAME OF FRAME WHERE ARE IMAGEBUTTONS"]

0

change this FULL NAME... to the full name of the C frame

0

There's no frame named just C, I'd like it to check for any frames with C at the beginning, like the frames C1 and C2, not sure how to do it.

0

Oh so there are placed in the gui? and in these frames are imagebuttons Alright let me update script

0

here You go. it might work now

0

Thanks a bunch for your help, I really appreciate it a lot. Does the bottom part need to be there twice because it seems to work with it there just once?

0

uhh keep it :) just for making sire that works. If its possible then I appreciate if You click "Accept answer" so dat will be nice :) thank You at all

0

Thank You very much

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