Make Fly Script for Mobile?

Hello!
I got this script from @EgoMoose to save time, where it activates flying for players. It works on PC, however when I try it on mobile, I couldn’t move it with the joystick. How would I go about that?
(Code isnt formatting correctly somehow)


local character = game.Players.LocalPlayer.CharacterAdded:Wait();
local hrp = character:WaitForChild("HumanoidRootPart");
local humanoid = character:WaitForChild("Humanoid");
local animate = character:WaitForChild("Animate");

while (not character.Parent) do character.AncestryChanged:Wait(); end
local idleAnim = humanoid:LoadAnimation(script:WaitForChild("IdleAnim"));
local moveAnim = humanoid:LoadAnimation(script:WaitForChild("MoveAnim"));
local lastAnim = idleAnim;

local bodyGyro = Instance.new("BodyGyro");
bodyGyro.maxTorque = Vector3.new(1, 1, 1)*10^6;
bodyGyro.P = 10^6;

local bodyVel = Instance.new("BodyVelocity");
bodyVel.maxForce = Vector3.new(1, 1, 1)*10^6;
bodyVel.P = 10^4;

local isFlying = false;
local isJumping = false;
local movement = {forward = 0, backward = 0, right = 0, left = 0};

-- functions

local function setFlying(flying)
	isFlying = flying;
	bodyGyro.Parent = isFlying and hrp or nil;
	bodyVel.Parent = isFlying and hrp or nil;
	bodyGyro.CFrame = hrp.CFrame;
	bodyVel.Velocity = Vector3.new();
	
	animate.Disabled = isFlying;
	
	if (isFlying) then
		lastAnim = idleAnim;
		lastAnim:Play();
	else
		lastAnim:Stop();
	end
end

local function onUpdate(dt)
	if (isFlying) then
		local cf = camera.CFrame;
		local direction = cf.rightVector*(movement.right - movement.left) + cf.lookVector*(movement.forward - movement.backward);
		
		if (direction:Dot(direction) > 0) then
			direction = direction.unit;
		end

		bodyGyro.CFrame = cf;
		bodyVel.Velocity = direction * humanoid.WalkSpeed * 3;
	end
end

local function onJumpRequest()
	if (not humanoid or humanoid:GetState() == Enum.HumanoidStateType.Dead) then
		return;
	end
	
	if (isFlying) then
		setFlying(false);
		isJumping = false;
	elseif (isJumping) then
		setFlying(true);
	end
end
	
local function onStateChange(old, new)
	if (new == Enum.HumanoidStateType.Landed) then	
		isJumping = false;
	elseif (new == Enum.HumanoidStateType.Jumping) then
		isJumping = true;
	end
end

local function movementBind(actionName, inputState, inputObject)
	if (inputState == Enum.UserInputState.Begin) then
		movement[actionName] = 1;
	elseif (inputState == Enum.UserInputState.End) then
		movement[actionName] = 0;
	end
	
	if (isFlying) then
		local isMoving = movement.right + movement.left + movement.forward + movement.backward > 0;
		local nextAnim = isMoving and moveAnim or idleAnim;
		if (nextAnim ~= lastAnim) then
			lastAnim:Stop();
			lastAnim = nextAnim;
			lastAnim:Play();
		end
	end
	
	return Enum.ContextActionResult.Pass;
end

-- connections

humanoid.StateChanged:Connect(onStateChange);
game:GetService("UserInputService").JumpRequest:Connect(onJumpRequest);

game:GetService("ContextActionService"):BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward);
game:GetService("ContextActionService"):BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward);
game:GetService("ContextActionService"):BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft);
game:GetService("ContextActionService"):BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight);

game:GetService("RunService").RenderStepped:Connect(onUpdate)```
1 Like

Maybe it’s because PlayerActions doesn’t support mobile. I don’t know and there is not much documentation on it.

1 Like

Please use proper code formatting, it’s hard to read the script clearly.
What’s wrong with the script, any errors?

1 Like
  1. The code somehow didn’t format correctly, it may be a bug.
  2. No errors at all in the output, the animation works, I just can’t move on mobile.

When I join the game on Mobile, the animations work fine, however when I try to move using the joystick, the player is just frozen in place, with the animation running.

Can you modify it? Maybe use MoveVectors instead. I find that easier and more accurate than using dot products

1 Like

Sorry, but may I ask what are MoveVectors? I’m new to scripting, so I don’t know about them.

It’s not a bug, you’re doing the formatting wrong. Please refer to roblox’s markdown guide.
It works on desktop correct? Are you using a local script? This likely isn’t something wrong with the flying script but instead the input for mobile if it works for desktop.

Mhm, works on Desktop, it’s a localscript. (Formatted Code)

local GetMoveVector = require(player:WaitForChild(“PlayerScripts”).PlayerModule:WaitForChild(“ControlModule”)):GetMoveVector() print(GetMoveVector)

That would get your move vectors. you can experiment with how they are formatted and use them to your advantage. Otherwise idk

2 Likes

Ok so where did you put the script exacly?

Thanks for formatting correctly.
It’s something wrong with the inputs not the flying mechanics.

For now let’s work on the connections portion of the script.

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")

humanoid.StateChanged:Connect(onStateChange)
UserInputService .JumpRequest:Connect(onJumpRequest)

ContextActionService:BindAction("forward", movementBind, false, Enum.PlayerActions.CharacterForward)
ContextActionService:BindAction("backward", movementBind, false, Enum.PlayerActions.CharacterBackward)
ContextActionService:BindAction("left", movementBind, false, Enum.PlayerActions.CharacterLeft)
ContextActionService:BindAction("right", movementBind, false, Enum.PlayerActions.CharacterRight)

RunService .RenderStepped:Connect(onUpdate)

I’d also like you to add print(actionName) on the line right after local function movementBind(actionName, inputState, inputObject) for debugging.

The output prints out “forward”, “backwards”, “left”, “right” on desktop, same thing for mobile.
EDIT: Somehow, clicking the Roblox Menu with a Mouse triggers all 4 actionNames, weird.

Does it print when you press those keys?

It should only print when you move, otherwise the connection is wrong.

Where did you put it? for me when i try your script and do what lynnlo said I don’t get a print at all but when i press movements keys on keyboard while testing on mobile then it fires and i could move

Yeah, it prints when I press the keys. W,A,S,D both trigger their repspective actionNames, same for Mobile.

Then it’s probably because it’s binded to wasd only which means the way you binded it doesn’t support mobile

Well then, how do I make it so it supports both platforms?? I’m not the person who made the script, @EgoMoose did, so I’m still trying to find out how the script works.

@zhenjie2003Alt had the right idea–I posted this solution over a year ago in another thread:

bit late for a reply, but your idea works and I would like to thank you for it. However you were a bit off for constant tracking I did it a little bit different.

local Player = game.Players.LocalPlayer

local MoveVector = require(script.Parent:WaitForChild(‘PlayerModule’):WaitForChild(“ControlModule”))

while wait() do

print(MoveVector:GetMoveVector())

end