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.