Adding mobile buttons to KeyCode

This is (one of) the scripts (local) in my plane:

local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")
local char = script.Parent
local human = char:WaitForChild("Humanoid")
local body = script:WaitForChild("body").Value
local rs = game:GetService("RunService")
local reps = game:GetService("ReplicatedStorage")

local bv = Instance.new("BodyVelocity", body)
local gyro = Instance.new("BodyGyro", body)

local max = 100000000

bv.MaxForce = Vector3.new(max, max, max)
gyro.MaxTorque = Vector3.new(max, max, max)

local flying = true

function eject()
	bv:Destroy()
	gyro:Destroy()
end

uis.InputBegan:Connect(function(key,chat)
	if not chat and key.KeyCode == Enum.KeyCode.E then
		if flying then
			flying = false
			bv.MaxForce = Vector3.new(0, 0, 0)
			gyro.MaxTorque = Vector3.new(0, 0, 0)
		else
			flying = true
			bv.MaxForce = Vector3.new(max, max, max)
			gyro.MaxTorque = Vector3.new(max, max, max)
		end
	end
end)

rs.RenderStepped:Connect(function(step)
	local cam = workspace.CurrentCamera
	bv.Velocity = body.CFrame.RightVector * 100
	gyro.CFrame = cam.CFrame * CFrame.Angles(0, math.rad(90), 0)
	local aav = body.CFrame:VectorToObjectSpace(body.AssemblyAngularVelocity)
	gyro.CFrame *= CFrame.Angles(-math.rad(aav.Y)*20, 0, 0)
end)

human.Died:Connect(eject)
body.Parent.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(eject)

How could i incorporate mobile buttons into this so that users on mobile can land the plane?

Could you not make it so u create mobile buttons and then you just make it where something might be a button on the keyboard, rather then that it’s linked to a mobile button.

I don’t understand

I’m trying to make it so that PC users can press E to fly/land but for mobile users there will be an addition button because they don’t have a keyboard

Create a mobile button and then make it so mobile have to press that button rather then the key on a keyboard.

1 Like

You need to connect that function to a button you made, you cant detect keyboard inputs without an actual keyboard.

1 Like

Try looking into ContextActionService:BindAction

Last week i was learning about ContextActionService and i think it automatically creates mobile buttons when you make keybinds. Im not sure tho since i got bad memory but you can check it out here

Ok, i managed to create the mobile buttons. The only problem is that the button is overlapping the jump button, is there any way i can change the button position?

Current script:

local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")
local char = script.Parent
local human = char:WaitForChild("Humanoid")
local body = script:WaitForChild("body").Value
local rs = game:GetService("RunService")
local reps = game:GetService("ReplicatedStorage")

local bv = Instance.new("BodyVelocity", body)
local gyro = Instance.new("BodyGyro", body)

local max = 100000000

bv.MaxForce = Vector3.new(max, max, max)
gyro.MaxTorque = Vector3.new(max, max, max)

local flying = true

function eject()
	bv:Destroy()
	gyro:Destroy()
end

local function toggle(name,state,object)
	if name == "Fly" and state == Enum.UserInputState.Begin then
		if flying then
			flying = false
			bv.MaxForce = Vector3.new(0, 0, 0)
			gyro.MaxTorque = Vector3.new(0, 0, 0)
		else
			flying = true
			bv.MaxForce = Vector3.new(max, max, max)
			gyro.MaxTorque = Vector3.new(max, max, max)
		end
	end
end

rs.RenderStepped:Connect(function(step)
	local cam = workspace.CurrentCamera
	bv.Velocity = body.CFrame.RightVector * 100
	gyro.CFrame = cam.CFrame * CFrame.Angles(0, math.rad(90), 0)
	local aav = body.CFrame:VectorToObjectSpace(body.AssemblyAngularVelocity)
	gyro.CFrame *= CFrame.Angles(-math.rad(aav.Y)*20, 0, 0)
end)

cas:BindAction("Fly",toggle,true,Enum.KeyCode.E,Enum.KeyCode.ButtonY)
human.Died:Connect(eject)
body.Parent.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(eject)

You can use the GetButton() function to edit a button, you can read more about it here

Yes, you can set the position like so:

ContextActionService:SetPosition(“NameOfButton”, UDim2.new(0.5,0,0.5,0)) — Sets the position of the button to the center of the screen.

Hope this helps :slight_smile:

https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction