Trying to get a switch to toggle from red to green

This is my second script I have made in ROBLOX since high school, so please forgive me if this sounds a little nooby. I am having a bit of difficulty getting this script to work. Ideally, I want to make it so that when a player clicks a button, a model of 4 parts turns from red to green and then back again. I suspect its some sort of syntax error on the last line, as the console says Really red, yellow, Purple 4x and “attempt to connect failed, passed button is not a function”. All this happens without me interacting with the button in any way.

local Button = workspace.PalisadeGatehouse.Doorbutton.Trigger.SurfaceGui.TextButton
local Gate = workspace.PalisadeGatehouse.PalisadeGate
local Crystal = workspace.PalisadeGatehouse.Doorbutton.DoorCrystal


function ColorFlip(ColoredPart,ColorSwap,ColorStart)

	local SetColor = ColoredPart.Part.BrickColor
	local FlippedC = "Toothpaste"
	print(SetColor)

if SetColor == ColorStart then
	FlippedC = ColorSwap
	print("green")
	
else
	FlippedC = ColorStart
	print("yellow")
	
	end

	for i, Piece in pairs(ColoredPart:GetChildren()) do
	Piece.BrickColor = BrickColor.new(FlippedC)
	print("purple")
	
	
	end

end

Button.MouseButton1Click:Connect(ColorFlip(Crystal,"Lime green","Really red"))

‘’’

Thank you in advance for any insight you can provide, as I’m a bit of a beginner. (Also ngl I’m a bit embarrassed by the fact my first question isn’t about inventory guis or cframing, but changing a buttons color.)

2 Likes

So basically, when you connect functions to events, you cannot pass the data directly to the function. Instead, the event will give you the parameters. If you’re about to connect a function to an event, you can hover over the parentheses, and it’ll tell you what it’ll give. For example, if you put your cursor on top of “()” in Event:Connect(), you’ll see it in the studio. Another way is to just use the auto-filled function the engine gives you, and it’ll automatically have the correct parameters.

To be honest, the function doesn’t need the parameters because everything you need is already defined for us so we’ll just use the variables you defined. Heres’s how i’d redo it.

local Button = workspace.PalisadeGatehouse.Doorbutton.Trigger.SurfaceGui.TextButton
local Gate = workspace.PalisadeGatehouse.PalisadeGate
local Crystal = workspace.PalisadeGatehouse.Doorbutton.DoorCrystal

local Colors = {
	Green = "Lime green",
	Red = "Really red"
}

local function ColorFlip()
	local PartColor = Crystal.Part.BrickColor
	
	local NewColor = nil
	
	if PartColor == Colors.Green then
		NewColor = Colors.Red
	else	
		NewColor = Colors.Green
	end
	
	for i, Piece in Crystal:GetChildren() do
		Piece.BrickColor = BrickColor.new(NewColor)
	end
end

Button.MouseButton1Click:Connect(ColorFlip)

Btw watch DevKing on YT he made a beginner tutorial it’s fire

I just want to make sure I’m understanding correctly. I can’t use a function in the parentheses next to connect if said function asks for some sort of input. Is that right?

The reason I had the code structured the way it was was in an attempt to make the code reusable elsewhere in the game should I need it later. (for example to change the color of a flag to red from blue or something)

also, thanks for the YouTube recommendation! I’ll check it out. Personally, I like browsing the forums more though. I am really good at reading, but picking up speech isn’t as easy for me.

No you can use a function but if the function your connecting doesn’t match with what the event is giving you, you’ll end up with errors.

Example:

local Players = game:GetService("Players")

local function PlayerAdded(Player)
	
end

local function PlayerAdded2(Player, PlayerData, PlayerName)
	
end	
	
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerAdded:Connect(PlayerAdded2)

The first function is correct because the PlayerAdded event gives you the player that joined the game.

The second function is incorrect because the PlayerAdded event will only give us the player that joined, not their data or name. If you tried printing playerdata or playername, you’d see nil because you weren’t given any of those.

If you wanted to make a reusable function that takes in multiple parameters, then you’d have to pass them in yourself.

Example:

local Players = game:GetService("Players")


local function DoCoolThingsWithPlayer(PlayerCharacter, PlayerName, PlayerID)
	
end	
	
local function PlayerAdded(Player)
	DoCoolThingsWithPlayer(Player.Character, Player.Name, Player.UserID)
end

Players.PlayerAdded:Connect(PlayerAdded)

Hey,

When using a function in the parentheses of another function, like mousebutton1click, you don’t put variables inside of that function because you are already passing the variables that come from mousebutton1click, into the ‘CoinFlip’ function.

For Example:

game.Players.PlayerAdded:Connect:function(playeradded)

This would pass the usual variables that you get from the playeradded function - the player instance.

I hope this clears the confusion up for you, if you need help or are still confused let me know.

Just making sure I get it. What mousebutton1whatever spits out is the only variable to be used for the function that is connected to mousebuttonwhatever. If I want to import other variables, I would have to call up another function inside the one connected to mousebuttonwhatever and do it that way. That sound right?

Also thank you both for all your help so far. I’m probably going to mark this as answer given tomorrow if it turns out I get everything, for now though I’m gonna sleep.

Yes exactly that for example:

Button.MouseButton1Click:Connect(function()
OtherFunctionName(variable1, variable2)
end)

Frick looks like I said solved too soon, there’s one more bug in the code, however the error message is gone. Imma see if I can find the mistake.

edit: Looks like for some reason SetColor ~= ColorStart I ended up just setting up a Boolean for the for statement. I’m a bit irked I can’t figure this out but whatever.