How can you end a Stepped Function if an if statement returns false?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? A fuel system where if your fuel goes to 0, you can’t control the boat

  2. What is the issue? The boat can still be controlled

  3. What solutions have you tried so far? ChatGPT and my brain

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Heres the code snippet.


local function fuelFinished()
	if currentFuel.Value == 0 then
		runService:UnbindFromRenderStep("DriveBoat")
		seat.ThrottleFloat = 0
	end
end

runService.Stepped:Connect(updateBoatUI)

runService.Stepped:Connect(fuelFinished)

while true do
	task.wait()
	if currentFuel.Value ~= 0 then
		runService:BindToRenderStep("DriveBoat", 1, DriveBoat)
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Using :Connect() on events return a Connection. If you save this to a variable, you can call :Disconnect() on it.

1 Like

The issue with your code is the fact that you are constantly binding new function to BindToRenderStep because you are not checking if it exists already.

Your implementation of BindToRenderStep is wrong. Although it will work, the priority you are setting is super low (Player input is 100).

What you could do instead is:

local boatControlConnection

local function fuelFinished()
	if currentFuel.Value == 0 then
		boatControlConnection  = boatControlConnection:Disconnect()

		seat.ThrottleFloat = 0
	end
end

runService.Stepped:Connect(updateBoatUI)

runService.Stepped:Connect(fuelFinished)

while task.wait() do
	if currentFuel.Value ~= 0 and not boatControlConnection then
		boatControlConnection = runService["Heartbeat"]:Connect(DriveBoat)

		-- //Use Heartbeat for stuff which will directly work with environment(physics, etc)
		-- //For UI, Camera control use RenderStepped
	end
end
1 Like

Right, so it runs out of fuel and as it does, if you hold the W key, it endlessly accelerates. Why?

You might need to set some kind of friction on the seat or something as such?
Not sure how vehicleSeats work

Man this is getting annoying…

hhhhh