Is there a way to optimize this script order type?

Hello. I have a problem that I have in my script.

I am making several different scripts that each make a specific action inside the game. The reason why I do this instead of putting them all together in a giant script is because of touched:Connect(function()).

On each script there’s a specific action that will be made by the left click, like in one it shoots a singular piece, then in another script it shoots another piece that has a different shape, and the piece uses completely different controls and occurs in a different manner. What I do to connect each script is this

local function passtonextpart()
    script.Parent.Round2.Disabled = false
    script.Disabled = true --[[this
disables the current round, round1, and makes it invalid until further activation]]
end

I do this in the end of every part, once every script’s functionality is completed this function is activated and the script shuts down. The issue with this is that I think it’s a bad model to follow on my script and that there’s better ways to achieve something better optimized.

I need a specific part of the script to happen once and then get disabled afterwards, the controls change every different round.

What do I do?

One way is to keep track of which state you’re in.

Do different things depending on the state, and then change the state.

Here’s a small example with four states: a “start”, two “rounds”, and a “finish” state, that will restart at the end. Hopefully you can see how you might extend this to work with your use case:

local gameState = "start" -- will only ever be "start", "round1", "round2", or "finish"

local function DoStart()
	print("Game began! Starting the first round...")
	wait(2)
	gameState = "round1" -- move to next state on the next iteration of the game loop
end

local function DoRound1()
	print("Round 1 START!")
	wait(1)
	print("Round 1 OVER! Moving to round 2...")
	gameState = "round2"
end

local function DoRound1()
	print("Round 2 START!")
	wait(1)
	print("Round 2 OVER! Moving to finish...")
	gameState = "finish"
end

local function DoFinish()
	print("The game is over!! Restarting soon...")
	wait(5)
	gameState = "start" -- restart on the next iteration of the game loop
end

while true do -- main game loop
	if gameState == "start" then
		DoStart() -- changes gameState to "round1" on the next loop!
	elseif gameState == "round1" then
		DoRound1()
	elseif gameState == "round2" then
		DoRound2()
	elseif gameState == "finish" then
		DoFinish()
	end
	-- jump back to the top and do the next state
end

I tried that, the issue is the :Connect event. The connect event launches whenever it’s called, it doesn’t matter where it is, whether it’s a function or 300 lines later. I need to make separate scripts because once a function with the connect is activated, it will activate itself again in the next function, changing for example the position of the camera.

Maybe there is no polish to it, maybe its fine the way I did it, I just don’t know the answer.

Thanks anyways, nobody was responding…

You can attach connections temporarily and Disconnect() them when you’re done:

local function DoRound1()
	print("Round 1 START")

	local temporaryConnection = workspace.Part.Touched:Connect(function()
		print("The Part was touched during round 1!")
	end)
	
	wait(10)

	temporaryConnection:Disconnect() -- close the connection

	gameState = "round2"
end

local function DoRound2()
	print("Round 2 START")

	local aDifferentConnection = workspace.Part.Touched:Connect(function()
		print("The Part was touched during round 2!")
	end)
	
	wait(10)

	aDifferentConnection:Disconnect() -- close the connection

	gameState = "finish"
end
2 Likes

Oh, DISCONNECT. that’s exactly what I was looking for! Thank you so much, man, I thought it was just connect.

1 Like

Oh and, let’s say hypothetically that I want to reset the game and start a new match. What do I do to start over from the beginning?

Just change the state back to the start

1 Like

That would just define the variable as start, maybe define that the game is on the start. What I’m saying is once the match is over, how do I go back to line 1 or line 30 (skipping but still using the definition of variables) without having to write everything again later on?

Wait, do I do a repeat loop?

Yes, it just changes the variable, but the main loop reads that variable to figure out what function to run:

1 Like

If you meant you wanted to reconnect the signal, put the connection in a function and call the function when needed.