Restart function when value changes

Hello, I currently have this script that is meant to turn on aircraft engines when Ctrl+E is clicked. It works fine, exept if the player leaves the seat when the function is running. This causes the function to start up automatically when the player gets back in the seat, and may result in some engines getting stuck. Seatvalue is 1 when player is sitting, and 0 when not. How can I immediately stop the function when Seatvalue changes to 0, and have the player manually restart it when they return to the seat? Thank you.

local key = game:GetService("UserInputService")
local Seatvalue = MainPlane.Seats.CaptainSeat.OwnerValue

key.InputBegan:Connect(function(Start)
	if Start.KeyCode == Enum.KeyCode.E and key:IsKeyDown(Enum.KeyCode.LeftControl) and EKey == false and Enginevalue == 0 and Seatvalue.Value == 1 then
		EKey = true
		MainPlane.Engine1Nozzle.Engine1Hinge.AngularVelocity = 20
		wait(10)
		MainPlane.EngineNozzles234.Engine2Hinge.AngularVelocity = 20
		wait(10)
		MainPlane.EngineNozzles234.Engine3Hinge.AngularVelocity = 20
		wait(10)
		MainPlane.EngineNozzles234.Engine4Hinge.AngularVelocity = 20
		EKey = false
		Enginevalue = 1
	end
end)
4 Likes

After every single wait you will need an if statement to check if the player is or is not sitting on the seat.

Change the wait()‘s to task.wait() instead first so it won’t throttle your code.

I don’t know much about task.wait() but I think it just doesn’t affect your server code, so it will check the condition even during the waits.

See if this works first, if it doesn’t, then have an if statement before each wait() to check if Seatvalue == 0

Tried both solutions, they do not fix anything. If a player leaves the seat and rejoins, the Seatvalue changes back to 1, and therefore the function fires automatically. Any way to reset it when the seatvalue changes?

I attempted this, it does not work for the following reason: If a player reenters the seat soon after, the Seatvalue will change back to 1, thereby automatically firing the function again, which is what I wish to prevent.

I would try replacing the waits with repeat until’s. Not the greatest way of doing it but I personally could not think of a better way of solving what you are trying to accomplish.

EDIT: You could also replace the task.wait(1) with something like task.wait(0.1) but you would also need to change the condition value for the loop to 100.

E.x.:

local key = game:GetService("UserInputService")
local Seatvalue = MainPlane.Seats.CaptainSeat.OwnerValue

key.InputBegan:Connect(function(Start)
	if Start.KeyCode == Enum.KeyCode.E and key:IsKeyDown(Enum.KeyCode.LeftControl) and EKey == false and Enginevalue == 0 and Seatvalue.Value == 1 then
		local t = 0
		EKey = true
		MainPlane.Engine1Nozzle.Engine1Hinge.AngularVelocity = 20
		repeat
			t += 1
			task.wait(1)
			if Seatvalue.Value == 0 then
				return
			end
		until t == 10
		MainPlane.EngineNozzles234.Engine2Hinge.AngularVelocity = 20
		t = 0
		repeat
			t += 1
			task.wait(1)
			if Seatvalue.Value == 0 then
				return
			end
		until t == 10
		MainPlane.EngineNozzles234.Engine3Hinge.AngularVelocity = 20
		t = 0
		repeat
			t += 1
			task.wait(1)
			if Seatvalue.Value == 0 then
				return
			end
		until t == 10
		MainPlane.EngineNozzles234.Engine4Hinge.AngularVelocity = 20
		EKey = false
		Enginevalue = 1
	end
end)

Unfortunatly does not work. When the player reenters the seat, the seatvalue changes back to 1, and the function fires automatically,.

Try doing what I said in the edit & replace the time interval between checks.

Same issue. When I enter the seat again, the variable changes back. Thus the function believes it is good to go.

I mean you could just reduce the time between checks to something so small its not possible to get back into the seat again in time. Something like 0.01 or 0.001 seconds (honestly if they are able to get back in that amount of time they deserve to keep their engines running lol)

The seat uses a proximity prompt, which takes at least 1.5 seconds. Therefore, it is impossible for them to get into the seat even in 1 second. However the function still fires, therefore the issue must be something else.

Might have something to do with return perhaps. I don’t have access to studio at the moment so I can’t test.

local key = game:GetService("UserInputService")
local Seatvalue = MainPlane.Seats.CaptainSeat.OwnerValue

key.InputBegan:Connect(function(Start)
	if Start.KeyCode == Enum.KeyCode.E and key:IsKeyDown(Enum.KeyCode.LeftControl) and EKey == false and Enginevalue == 0 and Seatvalue.Value == 1 then
		local t = 0
		EKey = true
		MainPlane.Engine1Nozzle.Engine1Hinge.AngularVelocity = 20
		repeat
			t += 1
			task.wait(0.1)
			if Seatvalue.Value == 0 then
				break
			end
		until t == 100
		if t ~= 100 then return end
		MainPlane.EngineNozzles234.Engine2Hinge.AngularVelocity = 20
		t = 0
		repeat
			t += 1
			task.wait(0.1)
			if Seatvalue.Value == 0 then
				break
			end
		until t == 100
		if t ~= 100 then return end
		MainPlane.EngineNozzles234.Engine3Hinge.AngularVelocity = 20
		t = 0
		repeat
			t += 1
			task.wait(0.1)
			if Seatvalue.Value == 0 then
				break
			end
		until t == 100
		if t ~= 100 then return end
		MainPlane.EngineNozzles234.Engine4Hinge.AngularVelocity = 20
		EKey = false
		Enginevalue = 1
	end
end)

Personally I would do something like this:

local key = game:GetService("UserInputService")
local Seatvalue = MainPlane.Seats.CaptainSeat.OwnerValue


local nozzles = { --Can be any name
	MainPlane.Engine1Nozzle.Engine1Hinge,
	MainPlane.EngineNozzles234.Engine2Hinge,
	MainPlane.EngineNozzles234.Engine3Hinge,
	MainPlane.EngineNozzles234.Engine4Hinge
}

key.InputBegan:Connect(function(Start)
	if Start.KeyCode == Enum.KeyCode.E and key:IsKeyDown(Enum.KeyCode.LeftControl) and EKey == false and Enginevalue == 0 and Seatvalue.Value == 1 then
		EKey = true
		for i = 1, #nozzles do
			if Seatvalue.Value ~= 1 then
				for i = 1, #nozzles do
					nozzles[i].AngularVelocity = 0 --Assuming 0, if not then just change I guess..
					--If you don't want it instant, then just add wait()
				end
				break
			end
			nozzles[i].AngularVelocity = 20
			wait(10)
		end
		EKey = false
		Enginevalue = 1
	end
end)