Hi!
How would I make many textButtons fire one function with different arguments passed?
I have 8 buttons that will change the player’s brick’s color. When you press each button, a single remote event will get fired sending the color to the server. The color that is passed to the remote event is different for each button. What is the best and cleanest way to achieve that?
(Note: I pass the color in the remote event because the colors are unlocked for everyone since the start so there’s no point checking if a player can use it)
One method would be to create a mapping between Colors and a function that you want to be called to handle that specific color. For example:
local ColorMap = {
Red = function(player)
print(player.Name .. " selected red")
end,
-- and do the rest with the other colors
}
RemoteEvent.OnServerEvent:Connect(function(player, color)
if ColorMap[color] then
ColorMap[color](player) -- call the function to handle the given color
end
end)
If you’re passing a Color3 as opposed to something like a color name, you can replace the keys in the ColorMap to the Color3 values themselves but you should be careful when doing this as if there is even a tiny variation (or potentially even if the hardware the player is on handles floating point numbers differently) it may give you a false negative and not call the function.
Thanks for the reply
I think you misunderstood. In my question I was asking about buttons. How would I connect the buttons to the remote event so that each button passes a different argument - the color code?
I am asking for the code on the client, not the server.
Ah. If you want to pass the Color3 value directly you could just call FireServer passing the color of the button directly. remoteEvent:FireServer(button.Color) although if you don’t want to pass the Color3 itself but another value, you can associate each button with a value you want to be automatically passed to FireServer (through the same mechanic in the original code I posted, mapping the Button Instances to the value you want to pass) and just pass that value when you want to call FireServer.
for _, button in pairs(Page:GetChildren()) do -- for each button
if button:IsA("TextButton") then
button.MouseButton1Down:Connect(function()
remoteEvent:FireServer(button.Color)
end)
end
end
How could I make the color mapping independent of the button’s properties? So all of the mapping would happen in the script. (Like, if the only different property was position)
You could actually map the instances themselves, that way you don’t rely on the Instance’s state at all:
local ColorMap = {
[Page.Button1] = "Red",
[Page.Button2] = "Blue",
-- and so on
}
-- and when you want to fire the remote event:
remoteEvent:FireServer(ColorMap[button])
This way the value passed to FireServer is completely independent of the properties of the buttons,
Although, I would like to give you some critisism that can hopefully improve your answers and questions, I’m in no way trying to offend you, I only want to help you improve. Here it is:
Most of your answers were really case-specific, that’s not a good practice, especially for beginners like me. People ask questions to get answers and use them for their case and in the future, but specific use cases are not reusable, which is why such answers aren’t great. For example, the last answer explained a nice reusable solution, but posts 4 and 6 only explained a solution specific for my specific use case, and I or any other developer wouldn’t learn anything from it.
Perhaps. I wasn’t entirely sure what you were asking at first (hence my first post). The main reason I provided code was to (try to) reinforce the idea I was getting across as I wasn’t sure if a general answer like “map the instances to the values you want associated with them” would actually help if someone was just learning. It might have, but for people who are just learning (at least in my experience) it seems a lot more useful to use concrete/specialized examples in order to get a more broad point across (i.e. the versatility of dictionaries). Although you’re right, people learn differently so I should start with the generalization to begin with and perhaps how they can use it for their specific use case.