This code takes the players choice from clicking a GUI button and changes everything within the character model named Color1 and Color2 to their respective choices. It connects to a server script that already has all Roblox pallet colors linked.
I would like to insert the other colors without spending hours retyping the color 4 times per function.
Is there any automated service out there that could do this for me?
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- GUI
local mainGui = script.Parent.Parent.Parent
local characterFrame = mainGui:WaitForChild("BoyCharacter")
-- RemoteEvent
local CustomizeEvent1 = ReplicatedStorage:WaitForChild("ColorEvent1")
local CustomizeEvent2 = ReplicatedStorage:WaitForChild("ColorEvent2")
-- Sounds
local sounds = ReplicatedStorage.Sounds
local selectSound = sounds.Selection
-----------------------------------------------------------------------------------------------------
--Body Color1
local Black1 = script.Parent.Parent.BodyChoiceC1.Black
Black1.Activated:Connect(function()
selectSound:Play()
CustomizeEvent1:FireServer("Black1")
end)
local White1 = script.Parent.Parent.BodyChoiceC1.White
White1.Activated:Connect(function()
selectSound:Play()
CustomizeEvent1:FireServer("White1")
end)
local Maroon1 = script.Parent.Parent.BodyChoiceC1.Maroon
Maroon1.Activated:Connect(function()
selectSound:Play()
CustomizeEvent1:FireServer("Maroon1")
end)
local Grey1 = script.Parent.Parent.BodyChoiceC1.Grey
Grey1.Activated:Connect(function()
selectSound:Play()
CustomizeEvent1:FireServer("Grey1")
end)
-----------------------------------------------------------------------------------------------------
--Body Color2
local kingdomgColor2 = script.Parent.Parent.BodyChoiceC2.Black
kingdomgColor2.Activated:Connect(function()
selectSound:Play()
CustomizeEvent2:FireServer("Black2")
end)
local Black2 = script.Parent.Parent.BodyChoiceC2.White
Black2.Activated:Connect(function()
selectSound:Play()
CustomizeEvent2:FireServer("White2")
end)
local Maroon2 = script.Parent.Parent.BodyChoiceC2.Maroon
Maroon2.Activated:Connect(function()
selectSound:Play()
CustomizeEvent2:FireServer("Maroon2")
end)
local Grey2 = script.Parent.Parent.BodyChoiceC2.Grey
Grey2.Activated:Connect(function()
selectSound:Play()
CustomizeEvent2:FireServer("Grey2")
end)
You could put all your colors inside a table and connect all buttons to their events with the color name:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- GUI
local mainGui = script.Parent.Parent.Parent
local characterFrame = mainGui:WaitForChild("BoyCharacter")
-- RemoteEvent
local CustomizeEvent1 = ReplicatedStorage:WaitForChild("ColorEvent1")
local CustomizeEvent2 = ReplicatedStorage:WaitForChild("ColorEvent2")
-- Sounds
local sounds = ReplicatedStorage.Sounds
local selectSound = sounds.Selection
-- Colors table
local colors = {"Black", "White", "Maroon", "Grey"}
local function ColorButtons(color, event, suffix)
local button = script.Parent.Parent["BodyChoiceC" .. suffix][color]
button.Activated:Connect(function()
selectSound:Play()
event:FireServer(color .. suffix)
end)
end
-- Connect Color1
for _, color in ipairs(colors) do
ColorButtons(color, CustomizeEvent1, "1")
end
-- Connect Color2
for _, color in ipairs(colors) do
ColorButtons(color, CustomizeEvent2, "2")
end