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.