I dont know why but when i place down the mobile button code it stops running my script for pc

So if tried to have two ways to press e, one on pc and on mobile but if i put these two together only the mobile button works on mobile but if i change to pc it does not run the repeat code.

local inputservice = game:GetService(“UserInputService”)
local contextActionService = game:GetService(“ContextActionService”)

inputservice.InputBegan:connect(function(i,g)
	    if i.UserInputType == Enum.UserInputType.Keyboard then
        if i.KeyCode == Enum.KeyCode.E then
		print("E PRESSED")
		Clicked = true
		end
	end
end)

local function onButtonPress()
print("E PRESSED")
Clicked = true
end
local mobilebutton = contextActionService:GetButton("SprintButton")
contextActionService:BindAction("SprintButton", onButtonPress, true, Enum.KeyCode.E)
contextActionService:SetPosition("SprintButton",UDim2.new(0.50,-25,0.20,-25))
contextActionService:SetImage("SprintButton","http://www.roblox.com/asset/?id=3202644510")


repeat 
	Line.Rotation = Line.Rotation + Speed
	wait()
until Clicked == true or Line.Rotation >= 360

maybe just check if the device is mobile?

if (inputservice.TouchEnabled) then
    -- mobile code
else
    -- non-mobile code
end

-- repeat

TouchEnabled can be returned as true for devices sporting touch screens. TouchEnabled itself is not adequate enough to determine what device a user is using. Part of the issue here is that OP didn’t check their console and realise that their code actually isn’t functional.

All of this can be done without the need for UserInputService at all, in fact.

local contextActionService = game:GetService("ContextActionService")

local function sprintAction(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        Clicked = true
    end
end

contextActionService:BindAction("Sprint", sprintAction, true, Enum.KeyCode.E)
contextActionService:SetPosition("Sprint", UDim2.new(0.5, -25, 0.2, -25))
contextActionService:SetImage("Sprint", "rbxassetid://3202644510")

repeat 
	Line.Rotation = Line.Rotation + Speed
	if not Clicked then
        wait()
    end
until Clicked == true or Line.Rotation >= 360

Handle both PC and mobile input in one fell swoop without relying on weird checks.

If you really want to check for mobile, it’s best to use GetLastInputType and check if the returned UserInputType is Touch or just accommodate for touch taps in your functions themselves.

3 Likes