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

Is it possible to use one function for GUI buttons located in a certain frame?

I am trying to do a color palette for a my game which includes a character customization and I was wondering if its possible to use one function instead of doing a function for each color options.

This is how the color palette looks like.

This is the only way that I've found.

1 answer

✓ Accepted answer

Usually, when you have several instances that essentially have the same functionality with very slight variations, you can use a for loop to encompass their similarities and variables within that loop to account for their differences. I'll give you an example of an efficient and user-friendly way to create such a system: Server Script:

local remote = game.ReplicatedStorage:WaitForChild('Remote Events/Functions') -- Folder in which I keep remote events and functions
for i,v in pairs(script.Parent:GetChildren()) do -- Do the following code for each child in Buttons
    if v:IsA('Part') then -- Make sure the given child is a part
	    v.Touched:Connect(function(part)
		    if not game.Players:GetPlayerFromCharacter(part.Parent) then return end -- If something other than a player touches the part, ignore it
		    local player = game.Players:GetPlayerFromCharacter(part.Parent)
		    remote.ButtonsMessage:FireClient(player,v.Message.Value) -- Fire the event for the player that touched the part with the part's unique message
		end)
	end
end

Local Script:

local remote = game.ReplicatedStorage:WaitForChild('Remote Events/Functions') -- Folder in which I keep remote events and functions
local player = game.Players.LocalPlayer
local screen = player.PlayerGui:WaitForChild('Screen') -- ScreenGui that will contain the message label
remote.ButtonsMessage.OnClientEvent:Connect(function(message) -- Perform the following function when the event fires
    screen.Message.Visible = true
    screen.Message.Text = message -- Display the message
end

Watch the demonstration here.

1 comment

0

Thanks it helped! I appreciate the fact that you took your time to make a video.

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