MouseButtonDown AND InputEnded not working

Hey there! I have this mobile controls script for our racing game, and very recently it just started failing. It has been working for months and nobody changed anything recently, it just simply started having issues, possibly due to a recent roblox update.

The issue: Whenever we press an arrow button in a quick action(pressing and immediately not pressing, a “tap”), the script cannot seem to catch the “Off” functions and thus the loop in the “On” functions keeps firing thinking a player is still pressing the button.

There are 4 buttons, the issue seems to be occurring only in the turning buttons. It will work when pressing for longer than around 0.3 seconds. Anything below that won’t fire it correctly.

local UserInputService = game:GetService("UserInputService")
local throttleMouse = false
local brakeMouse = false
local leftMouse = false
local rightMouse = false
local butttons = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls
local throttle = butttons.Throttle
local brake = butttons.Brake
local left = butttons.Left
local right = butttons.Right

function throttleOn()
	throttle.MouseButton1Down:Connect(function()

		throttleMouse = true
		local direction = "Forward"
		local confirm = true
		while throttleMouse == true do

			game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
			task.wait()
		end	
	end)
end	
function throttleOff()
	throttle.InputEnded:Connect(function()
		
		throttleMouse = false
		local direction = "Forward"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
end	
function brakeOn()
	brake.MouseButton1Down:Connect(function()

		brakeMouse = true
		local direction = "Backward"
		local confirm = true
		while brakeMouse == true do
			game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
			task.wait()
		end	
	end)
end
function brakeOff()
	brake.InputEnded:Connect(function()
		
		brakeMouse = false
		local direction = "Backward"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
end	
function leftOn()
	left.MouseButton1Down:Connect(function()

		leftMouse = true
		local direction = "Left"
		local confirm = true
		while leftMouse == true do
			game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
			task.wait()
		end	
	end)
end
function leftOff()
	left.InputEnded:Connect(function()
		
		leftMouse = false
		local direction = "Left"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
	
	left.MouseButton1Up:Connect(function()

		leftMouse = false
		local direction = "Left"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
end	
function rightOn()
	right.MouseButton1Down:Connect(function()

		rightMouse = true
		local direction = "Right"
		local confirm = true
		while rightMouse == true do
			game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
			task.wait()
		end	
	end)
end
function rightOff()
	right.InputEnded:Connect(function()
		
		rightMouse = false
		local direction = "Right"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
	
	right.MouseButton1Up:Connect(function()

		rightMouse = false
		local direction = "Right"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
end	

throttleOn()
throttleOff()
brakeOn()
brakeOff()
leftOn()
leftOff()
rightOn()
rightOff()

function onChange()
	task.wait(3)
	local newThrottle = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls.Throttle
	local newBrake = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Brake
	local newLeft = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Left
	local newRight = script.Parent.Parent.PlayerGui.ScreenGui.MobileControls.Right
	butttons = script.Parent.Parent.PlayerGui:WaitForChild("ScreenGui").MobileControls
	throttle = newThrottle
	brake = newBrake
	left = newLeft
	right = newRight
	throttleMouse = false
	brakeMouse = false
	leftMouse = false
	rightMouse = false

	throttleOn()
	throttleOff()
	brakeOn()
	brakeOff()
	leftOn()
	leftOff()
	rightOn()
	rightOff()
end
game.ReplicatedStorage.DeactivateMControls.OnClientEvent:Connect(onChange)

We were using input ending for all the “Off” functions, but we used mousebutton1up as well and neither worked.

The localscript sends a message to this main script

local carData = require(game.ReplicatedStorage.CarData)

local carTable = carData.Cars
local carValues = carData.CarsValues
local start = {}

function onChangeDirection(player, direction, confirm)
	local newCar
	
	for _, car in ipairs(workspace.RaceCars:GetChildren()) do
		if car:FindFirstChild("Player") and car.Player.Value == player.Name then
			newCar = car
			break
		end
	end
	
	if newCar then
		local engine = newCar.VehicleSeat
		
		if direction == "Forward" then
			if confirm == true then
				engine.Throttle = 1
			elseif confirm == false then
				engine.Throttle = 0
			end
		elseif direction == "Backward" then
			if confirm == true then
				engine.Throttle = -1
			elseif confirm == false then
				engine.Throttle = 0
			end
		elseif direction == "Left" then
			if confirm == true then
				engine.Steer = -1
			elseif confirm == false then
				engine.Steer = 0
			end
		elseif direction == "Right" then
			if confirm == true then
				engine.Steer = 1
			elseif confirm == false then
				engine.Steer = 0
			end
		end
	end
end
game.ReplicatedStorage.CarThrottle.OnServerEvent:Connect(onChangeDirection)

We believe this has nothing to do with it though, as it seems like a performance bug related to the client.

We don’t know what could be causing this, if it is our code or a roblox update. Whichever it is, we would love to be guided through why this is happening.

Is this a local script or a server sided one?

The first script is local in the starterplayerscripts, the second is in the server

I don’t think you can use a Connect() in a function. Try removing the function definition around it.

That is the alternate version of a function and is the method you use when you want to call if from multiple sources, not that it was in this script it is just a habit.

Edit oh actually there are multiple spots you could be talking about the Connect, if you are referring to the one in each function, I don’t know how else are you supposed to write it, I can’t think of one without it.

This, for example

function leftOff()
	left.InputEnded:Connect(function()
		
		leftMouse = false
		local direction = "Left"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
	
	left.MouseButton1Up:Connect(function()

		leftMouse = false
		local direction = "Left"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
	end)
end	

Could be written as this:

left.InputEnded:Connect(function()
		
		leftMouse = false
		local direction = "Left"
		local confirm = false
		game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
end)
	
left.MouseButton1Up:Connect(function()

	leftMouse = false
	local direction = "Left"
	local confirm = false
	game.ReplicatedStorage.CarThrottle:FireServer(direction, confirm)
end)

Edit: I could be wrong, but this is how I personally write my code if it requires a function like this.

They need to be inside a second function so they can be called again, because whenever the player resets it won’t reference the buttons that were deleted because it can’t. So when I recall the function it finds the new buttons.

You can use Connect() in anyscope im pretty sure thats find.

Thinng dont just break down for no reason. There has to be a reason for the change.

Do the cars work with roblox physics or no physics? If not, the only thing i can think of trying is making the controls all client sided and seeing if the error goes away.