Function continuing to run even after disconnected

So i have a function running for my plane/cart hybrid, that for some reason, keeps running even after disconnection.

local cartSpawned = game:GetService("ReplicatedStorage"):WaitForChild("SpawnCar")
local carEvent = game:GetService("ReplicatedStorage"):WaitForChild("CarEvent")

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")

cartSpawned.OnClientEvent:Connect(function(car)
	print("SpawnedCar")
	
	local carUI = game.Players.LocalPlayer.PlayerGui:WaitForChild("CarUI")
	carUI.Enabled = true
	
	local toggle = carUI.Toggle.Value
	
	local seat = car:WaitForChild("Seat")
	
	local throttled = false
	local throttle = 0
	math.clamp(throttle, 0, 1)
	
	local speed = 0
	local speedCap = carUI.SpeedControl.OutputValue
	
	local connection
	
	uis.InputBegan:Connect(function(key, processed)
		print("InputBegan")

		if seat.Occupant and not processed and key.KeyCode == Enum.KeyCode.W then	
			print("PassedChecks")
			
			if toggle.Value == false then
				throttled = true
				throttle += 0.1
			else
				throttled = not throttled
			end
			
			connection = rs.PreRender:Connect(function(dt)
				
				local capValue = speedCap.Value*100
				
				speed += ((capValue/60)*throttle)*dt
				if speed > capValue then speed = capValue end
				
				throttle += 0.2*dt
				if throttle >= 1 then throttle = 1 end
				
				if throttled == false then
					speed -= ((capValue/2)*(1-throttle))*dt
					
					throttle -= 1*dt
					if throttle <= 0 then throttle = 0 end
					
					if speed <= 0 then
						speed = 0
						throttle = 0
						
						print("StoppedCart", throttled, connection.Connected)
						connection:Disconnect()
					end
				end
			end)
		end
	end)
	
	uis.InputEnded:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.W then
			if toggle.Value == true then return end
			throttled = false
		end
	end)
	rs.PreRender:Connect(function(dt)
		local velocity = speed * car.CFrame.LookVector
		car.CFrame = CFrame.new(car.Position + velocity*dt) * car.CFrame.Rotation
	end)
end)

Yes, this is all running on the client… I will fix it later, i prefer coding on a single script while im still making it.
Edit: The function that keeps running is the run service pre render, but only while toggle is true, for some reason.

That’s because you’re using one shared ‘connection’ variable for all connections. Everytime you click W, it’s changing to the newest prerender connection - which means if you spam the key before the speed reaches 0, it wont disconnect that function.

Put ‘local connection’ on the line above when you set it like this, so it cant be overwritten.

local connection
connection = rs.PreRender:Connect(function(dt)
2 Likes

When input begins if toggle value == true then throttled value becomes not throttled.
– “False → true, true → false” When you said it only happens when toggle is true, throttled is set to true, because when input ends throttled is set to false. So your if statement if throttled == false then is never ran.

When input ends if toggle is false, throttled = false, when input begins if toggle is false then throttled is the opposite of throttled–( True ) the connection can only disconnect when throttled == false.
– Throttled is always true when toggle is false, thus the connection never disconnects
edit I read that wrong

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.