They’re all ordered in numerical order, as they should be. Here’s the code (only the first two buttons are scripted):
local HairColorButtonArray = workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.GuiMasterParent.HairSelectionMainframe.HairColors:GetChildren()
local ClickSound = workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.Click
HairColorButtonArray[1].MouseButton1Click(function()
ClickSound:Play()
print("Come out to the coast, we'll get together, have a few laughs.")
end)
HairColorButtonArray[2].MouseButton1Click(function()
ClickSound:Play()
print("Yippee ki yay motherflipper.")
end)
I’m not getting any error messages as to why it’s not working, and they don’t seem to be printing the Die Hard quotes that I put in. Does my script just hate the movie Die Hard? Does anyone know how to fix this code?
Instead of going through each button one by one, you can use a for loop, to go through all the children of the Folder, which I assume is all ImageButtons, then use a MouseButton1Click event on all collected children.
local HairColorButtonArray = workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.GuiMasterParent.HairSelectionMainframe.HairColors:GetChildren()
local ClickSound = workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.Click
for i , v in pairs(HairColorButtonArray) do
if v:IsA("ImageButton") then
v.MouseButton1Click:Connect(function()
ClickSound:Play()
print(v.Name .. " has been click.") -- Debug
end)
end
end
That does sound like a good idea, but I need to distinguish which button is being clicked. Each button fires a different server event. How would I distinguish the buttons?
As in the script, when you print v.Name it will print the Button that you click, so I would suggest to have one remote for all the hair, then send what Button was clicked through the arguments inside of the fired data. I also don’t recommend you use a Remote-event for each hair as it’s not secure and is over-complicated.
So you’re saying I should use one RemoteEvent for all the hair colors and use that as a filter of sorts to transmit the button data? I’m not quite sure how to do that, would I do
if v.Name = "01HairColor" then
game.ReplicatedStorage.Events.HairColorEvent:FireServer()
end
Even so, one event wouldn’t be able to transmit which button is being clicked, it’d just fire a universal server event.
When you do :FireServer on RemoteEvent, it’ll automatically send the player itself as argument 1, and an extra information for argument 2. Please have a read for more understanding.
That would be redundant. This could all be done rather easily with only one Remote Event. If all your Image Buttons are to only change a character’s hair color, you could add a string value of the color you want it to be.
So instead of having to type an if statement for every single Image Button, you could easily just type:
for c, child in pairs(workspace.SettlerLobby.ScriptedObjects.MasterCCDisplay.MainDisplay.GuiMasterParent.HairSelectionMainframe.HairColors:GetChildren()) do
child.MouseButton1Click:Connect(function() -- This will always work provided you have a color name for every Image Button
ClickSound:Play()
game.ReplicatedStorage.Events.HairColorEvent:FireServer(child.hairColor.Value)
end
end
On the Remote Function script (or wherever it is), you could do something like this:
local hairColors = {
White = Color3.new(1, 1, 1),
Black = Color3.new(0, 0, 0)
--- etc.
}
workspace.Event.OnServerEvent:Connect(function(Player, color) --- With color being the color that the player sent to the server.
if table[color] then -- Instead of having all of those if statements, this would be your only one.
Player.Hair.Color = table[color]
end