Touched Event disables when turn key pressed

I have a speedboost that when touched by my car enables an increase in the BodyThrust of the car for 3 seconds then returns it to normal speed… however if i try to turn the car during this boost, it disables the touched event and the car returns to normal speed upon me turning the car. How can I keep the touched event going while also turning the car? Keep in mind if i enter the boost area with my car already turning and i dont let go, it will keep the boost… basically however i enter the boost must be held in order for me to maintain the boost, anything i press during the boost will disable the boost. The script below:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit:FindFirstAncestor("Car") then 
			print("hit")
			local Car = hit.Parent
			local Motor = Car.Engine
			Motor.Thrust.Force = Vector3.new(-Car.DriverSeat.Throttle * Car.DriverSeat.Torque * 45000,0,0)
			wait(3)
			Motor.Thrust.Force = Vector3.new(-Car.DriverSeat.Throttle * Car.DriverSeat.Torque * 20000,0,0)
		else
		end
		wait(1)
		debounce = false
	end
end)

Script for turning the car?

This is the script for controlling the car:

function operate()
	if script.Parent.Active.Value == true then
		if script.Parent.DriverSeat.Throttle ~= 0 then
			script.Parent.Manouver.Thrust.force = Vector3.new(0,script.Parent.DriverSeat.Throttle * script.Parent.DriverSeat.Steer * script.Parent.DriverSeat.TurnSpeed *1250,0)
		else
			script.Parent.Manouver.Thrust.force = Vector3.new(0,script.Parent.DriverSeat.Steer * script.Parent.DriverSeat.TurnSpeed * 450,0)
		end
		local drive = script.Parent.DriverSeat.Throttle * script.Parent.DriverSeat.Torque * 12000
		script.Parent.Engine.Thrust.force = Vector3.new(-drive,0,0)
	else
	end
end

script.Parent.DriverSeat.Changed:connect(operate)

Are they in the same script?

The speedboost is in a part… that part has the first script for the boost. Its a speedboost pad that once touched activates the speed in the vehicle touching it.
The second script is in the vehicle.

Both of these are conflicting, you need to find a way to fix this.

Maybe do so by making debounce a BoolValue instance and checking in both scripts if debounce is true.

This is why i have:

local Motor = Car.Engine

That’s not how scripting works. They both are accessing and changing the ThrustForce of the same thing. Or so it seems.

I have no issue with the thrust… the vehicle boosts when it touches the pad… the issue is that if i try to turn the vehicle during the boost, it stops the touched function.

I just gave a fix for that, please re-read what I said about the BoolValue.

Will the Active value suffice? maybe i just add that to the touched function?

What do you mean by “Will the Active value suffice”?

Since i have this above in my vehicle script, this is my bool value that i can add to the touched event?

If it is not in current use, yes. Remember, the same BoolValue must be accessed on both scripts.

1 Like

Ok let me try that out. . . . .

No it doesn’t change anything when i did this:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if debounce == false then
		debounce = true
		if hit:FindFirstAncestor("Car") then 
			print("hit")
			local Car = hit.Parent
			local Motor = Car.Engine
            if Car.Active.Value == true then
			Motor.Thrust.Force = Vector3.new(-Car.DriverSeat.Throttle * Car.DriverSeat.Torque * 45000,0,0)
			wait(3)
			Motor.Thrust.Force = Vector3.new(-Car.DriverSeat.Throttle * Car.DriverSeat.Torque * 20000,0,0)
        end
		else
		end
		wait(1)
		debounce = false
	end
end)

Edit: or should i be setting the value to true instead of making it an if then?

script.Parent.Touched:Connect(function(hit)
	if not Car.Active.Value then
		Car.Active.Value = not Car.Active.Value
		if hit:FindFirstAncestor("Car") then 
			print("hit")
			local Car = hit.Parent
			local Motor = Car.Engine
			Motor.Thrust.Force = Vector3.new(-Car.DriverSeat.Throttle * Car.DriverSeat.Torque * 45000,0,0)
			wait(3)
			Motor.Thrust.Force = Vector3.new(-Car.DriverSeat.Throttle * Car.DriverSeat.Torque * 20000,0,0)
		end
		wait(1)
		Car.Active.Value = not Car.Active.Value
	end
end)
function operate()
    if script.Parent.DriverSeat.Throttle ~= 0 then
		script.Parent.Manouver.Thrust.force = Vector3.new(0,script.Parent.DriverSeat.Throttle * script.Parent.DriverSeat.Steer * script.Parent.DriverSeat.TurnSpeed *1250,0)
	else
		script.Parent.Manouver.Thrust.force = Vector3.new(0,script.Parent.DriverSeat.Steer * script.Parent.DriverSeat.TurnSpeed * 450,0)
	end
    local drive = script.Parent.DriverSeat.Throttle * script.Parent.DriverSeat.Torque * 12000
    if not script.Parent.Active.Value then
	    script.parent.engine.Thrust.Force = Vector3.new(-drive,0,0)
    end
end

script.Parent.DriverSeat.Changed:Connect(operate)

Try this… and make sure Active isn’t being used anywhere else.

1 Like

Wait… I think I firgot a bit of code, don’t test yet.

Feel free to test it out now, and notify me of any errors.

1 Like

Problem is that the variable Car hasnt been defined yet, it gets defined by the touch event.