Hey all, got a really annoying one!

I am currently making a hill climb racing kinda game and It currently supports multiple devices, Including mobile this is where an issue occurs. When holding the button for left or right it does actually register however if you hold both at the same time and let go they get stuck and the input keeps registering. This problem does not occur on PC or Xbox however you are still able to press the UI Buttons with the keybinds/triggers

local driving = 0
if script.Parent.Driving.Right:GetState() and not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
			
	driving += 1
	--CAR CODE HERE

if script.Parent.Driving.Left:GetState() and not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
			
	driving += 1
	--CAR CODE HERE
end
1 Like

The simplest solution is to make your conditions impossible to run at the same time, i.e., mutually exclusive. This is easily accomplished by changing if to elseif (and removing the previous end).

local driving = 0
if script.Parent.Driving.Right:GetState() and not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
			
	driving += 1
	--CAR CODE HERE
--> change if to an elseif
elseif script.Parent.Driving.Left:GetState() and not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
			
	driving += 1
	--CAR CODE HERE
end

However, the professional approach is to have direction be a numerical value. This lets your code be a lot more flexible and simplified going further.

local driving = script.Parent.Driving
local left = if driving.Left:GetState() then 1 else 0
local right = if driving.Right:GetState() then 1 else 0
-- If player presses left, then it's 1, if player presses right, it's -1, if player presses both or neither, it's 0
local direction = left - right

With this direction value, doing rotation (or whatever you need to do) is much simpler.

car.AssemblyAngularVelocity = CFrame.fromEulerAngles(direction, 0, 0) -- A very basic example
Et cetera

You’re duplicating the same condition twice. Just put that condition outside and wrap everything.

local driving = 0
--> These conditions apply to both states, so move them out
if not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
	if script.Parent.Driving.Right:GetState() then
		
		driving += 1
		--CAR CODE HERE
	elseif script.Parent.Driving.Left:GetState() then
			
		driving += 1
		--CAR CODE HERE
	end
end

Also, you duplicate the same code for driving (I assume), which can introduce copying errors. This was already addressed before, but assuming you choose some other direction, consider this structure, which places the duplicated code outside.


local driving = 0
if not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
	if script.Parent.Driving.Right:GetState() then
		
		driving += 1
	elseif script.Parent.Driving.Left:GetState() then
			
		driving += 1
	end

	if driving then --> Car code is deduplicated. Easier to maintain.
		-- CAR CODE HERE
	end
end
2 Likes

Okay I understand will this fix the issue where the player holds both and lets go at the same time it will continue to register after it has been released?

Im now going to test the buttons on mobile, Edit: Just tested it is still sticking if you would like I can send you the games link? This only happens in game when driving feel free to load any map and see what I mean mobile buttons get stuck when both are pressed and released at the same time

In that case, we might need to see more of the code to understand the problem.

Here is just the nessasary stuff that has to do with driving (To save yourself from looking at my flip calculations)

-- handle car movement
		local manouverForce: number = MainGame.CarManouverability * delta * 60
		if not isCarGrounded then
			manouverForce *= 3
		end
		
		local leftState = script.Parent.Driving.Left:GetState() -- these are roblox's new input action context instances
		local rightState = script.Parent.Driving.Right:GetState()
		
		local rightDir = if rightState then MainGame.CarTorque * delta * 60 else 0
		local leftDir = if leftState then MainGame.CarTorque * delta * 60 else 0
		local rightMan = if rightState then Vector3.new(0,0,math.rad(-manouverForce)) else Vector3.new(0,0,0)
		local leftMan = if leftState then Vector3.new(0,0,math.rad(manouverForce)) else Vector3.new(0,0,0)
		
		local direction: number = leftDir - rightDir
		local directionMan: number = leftMan - rightMan

		car.PrimaryPart:ApplyAngularImpulse(-directionMan)
		if car.PrimaryPart.AssemblyLinearVelocity.Magnitude <= MainGame.MaxSpeed then
			for _, wheel: BasePart in pairs(wheels) do
				wheel:ApplyAngularImpulse(Vector3.new(0,0,math.rad(-direction)))
			end
		end
		
		if direction > 0 or direction < 0 then
			
			MainGame.FuelManager.UseFuel(delta, MainGame)
			car.Engine.PlaybackSpeed = math.lerp(car.Engine.PlaybackSpeed, 1.8, 0.05)
		else
			car.Engine.PlaybackSpeed = math.lerp(car.Engine.PlaybackSpeed, 1, 0.05)
		end

edit: forgot to mension its in a PreSimulation loop with the delta variable passed

I’m not really sure since I don’t really have an easy way to debug, but it seems like the on-screen controls are a bit odd with touch input. Do you have any code relating to those?

Not really just

local leftState = script.Parent.Driving.Left:GetState() -- these are roblox's new input action context instances
local rightState = script.Parent.Driving.Right:GetState()

Using roblox’s new input action system
however I do enable and disable them but this was an issue before I even started doing that for diffrent game states

This is still an ongoing issue and i really need help with this, please…

I should’ve said something, but I just can’t really find a reason for this to occur. You might need to use some debug prints/UI elements to display the state of everything to see where it trips up. I had a guess it could theoretically be some internal issue with Roblox or some sort of issue with your touchscreen input, but I just don’t know.

I actually dug way deep into the forums of the post about input contex tinstances and fiund out that it may be intended and i really think it shouldn’t be…