Move where the player look (creating fly)

Hello, I would like to know how can I move where I look with W and S?

local plr = game:GetService("Players").LocalPlayer

local uis = game:GetService("UserInputService")

local Moving = false

local function enableNoClip(char, doEnable) 
	for i, part in ipairs(char:GetChildren()) do 
		if part:IsA("Part") or part:IsA("MeshPart") then 
			part.CanCollide = doEnable
			if not doEnable then 
				part.Anchored = true
			else 
				part.Anchored = false
			end
		elseif part:IsA("Accessory") then 
			for k, p in ipairs(part:GetChildren()) do
				if part:IsA("Part") or part:IsA("MeshPart") then 
					part.CanCollide = doEnable
				end
			end 
		end
	end
end


uis.InputBegan:Connect(function(input, process) 
	if input.KeyCode == Enum.KeyCode.W then 
		local char = plr.Character
		local isAble = plr.AdminTerm.isFlying.Value
		if isAble then 
			if Moving then return end 
			
			enableNoClip(char, false)
			Moving = true
			while Moving == true do
				char:PivotTo(char:GetPivot() * CFrame.new(0,0,-1))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.S then 
		local char = plr.Character
		local isAble = plr.AdminTerm.isFlying.Value
		if isAble then 
			if Moving then return end 

			enableNoClip(char, false)
			Moving = true
			while Moving == true do
				char:PivotTo(char:GetPivot() * CFrame.new(0,0,1))
				task.wait(0.01)
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, _gameProceesedEvent)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		local isAble = plr.AdminTerm.isFlying.Value
		if isAble and Moving then
			enableNoClip(plr.Character, true)
			Moving = false
		end
	end
end)
3 Likes

You can use a VectorForce and set the vector force to Camera.CFrame.LookVector

So I take the character model and pivot to the LookVector?

character:PivotTo(character:GetPivot() * workspace.CurrentCamera.CFrame.LookVector)

If you’re trying to make a player fly, then you have to anchor the player first, with their arms and legs in a default position. Then, they use UserInputService to check when a player clicks the WASD keys to move. Then, that’s sent through a RemoteEvent to then move the player, relevant to their CFrame.LookVector.

However, I want to try a simpler way. Let’s make all of this server-sided:


First I need you to try and experiment with something. Put this script inside ServerScriptService and tell me what prints out:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
	
		for _,limb in pairs(character:GetChildren()) do --Anchor the player
			if limb:IsA("BasePart") then
				
				limb.Anchored = true
				
				limb:GetPropertyChangedSignal("Anchored"):Connect(function()
					limb.Anchored = true
				end)
				
			end
		end
		
		character.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			print(character.Humanoid.MoveDirection)
		end
		
	end)
end)

I want to see if the server script is able to check if a player moves their WASD keys, without using a client script’s UserInputService check. Tell me what prints.

Look the complete code :> at the first post.

Oh.


I would use a RunService.RenderStepped loop, which checks if the WASD keys are down, using UIS:IsKeyDown(). Just checking if an input has been detected doesn’t work well, if your script is checking if you’re constantly flying forwards.

local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

RS.RenderStepped:Connect(function(delta)
	if UIS:IsKeyDown(Enum.KeyCode.W) then print("W") end
	if UIS:IsKeyDown(Enum.KeyCode.A) then print("A") end
	if UIS:IsKeyDown(Enum.KeyCode.S) then print("S") end
	if UIS:IsKeyDown(Enum.KeyCode.D) then print("D") end
end)

Another thing, could you actually try printing the humanoid’s MoveDirection and see if it does respond, even if your character is anchored? If it does work, then life gets much more easier.

1 Like

Yeah but to move the character to where he is looking when I dectect a right click and move normally like if you were as a spectator, how I do?

If you want to move a character forwards, it’s as simple as this:

--In this example, if the player messages "move", then the player teleports forwards

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Chatted:Connect(function(message)
			if message == "move" then
				character:PivotTo(character:GetPivot() + (character.HumanoidRootPart.CFrame.LookVector * 5))
			end
		end)
	end)
end)

Alright thanks I will try it soon.

Well it does not work :(, my character don’t turn.

https://gyazo.com/b692618298f615da286399956e3c3597

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local uis = game:GetService("UserInputService")

local Moving = false

local function enableNoClip(char, doEnable) 
	for i, part in ipairs(char:GetChildren()) do 
		if part:IsA("Part") or part:IsA("MeshPart") then 
			part.CanCollide = doEnable
			if not doEnable then 
				part.Anchored = true
			else 
				part.Anchored = false
			end
		elseif part:IsA("Accessory") then 
			for k, p in ipairs(part:GetChildren()) do
				if part:IsA("Part") or part:IsA("MeshPart") then 
					part.CanCollide = doEnable
				end
			end 
		end
	end
end


uis.InputBegan:Connect(function(input, process) 
	local char = plr.Character
	local isAble = plr.AdminTerm.isFlying.Value
	if input.KeyCode == Enum.KeyCode.W then 
		if isAble then 
			if Moving then return end 
			
			enableNoClip(char, false)
			Moving = true
			while Moving  do
				char:PivotTo(char:GetPivot() * CFrame.new(0,0,-1))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.S then 
		if isAble then 
			if Moving then return end 

			enableNoClip(char, false)
			Moving = true
			while Moving do
				char:PivotTo(char:GetPivot() * CFrame.new(0,0,1))
				task.wait(0.01)
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, _gameProceesedEvent)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		local isAble = plr.AdminTerm.isFlying.Value
		if isAble and Moving then
			enableNoClip(plr.Character, true)
			Moving = false
		end
	end
end)

mouse.Button2Down:Connect(function()
	local isAble = plr.AdminTerm.isFlying.Value
	local char = plr.Character
	if isAble then
		char:PivotTo(char:GetPivot() + (char.HumanoidRootPart.CFrame.LookVector * 5))
	end
end)

Janky Flight System

--[[ Local Script ]]--
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local RunSerivce = game:GetService("RunService")
local localPlayer = Players.LocalPlayer

local camera = game.Workspace.CurrentCamera

local flightEnabled = false
local LinearVelocity = nil
local alignOrientation = nil
local function canFly(enabled: boolean)
	local function disableFlight()
		ContextActionService:UnbindAction("FlightForward")
		ContextActionService:UnbindAction("FlightBackward")
		ContextActionService:UnbindAction("FlightLeft")
		ContextActionService:UnbindAction("FlightRight")
		RunSerivce:UnbindFromRenderStep("Fly")
		
		if LinearVelocity then
			LinearVelocity:Destroy()
		end
		
		if alignOrientation then
			alignOrientation:Destroy()
		end
		
		flightEnabled = false
	end
	
	if enabled then
		if flightEnabled then return end
		flightEnabled = true
		local character = localPlayer.Character
		if not character then return end
		local HumanoidRootPart = character.HumanoidRootPart
		local Humanoid: Humanoid = character.Humanoid
		
		LinearVelocity = Instance.new("LinearVelocity")
		LinearVelocity.Parent = HumanoidRootPart
		LinearVelocity.Attachment0 = HumanoidRootPart.RootAttachment
		LinearVelocity.MaxForce = math.huge
		LinearVelocity.Name = "FlightLV"
		
		alignOrientation = Instance.new("AlignOrientation")
		alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
		alignOrientation.Attachment0 = HumanoidRootPart.RootAttachment
		alignOrientation.MaxTorque = math.huge
		alignOrientation.Responsiveness = 200
		alignOrientation.Parent = HumanoidRootPart
		alignOrientation.Name = "FlightAO"
		
		local flying = true
		local movementLog = {}
		local function logMovement(name:string, playerAction: EnumItem)
			return name, (function(actionName, inputState, inputObject)
				movementLog[playerAction] = inputState == Enum.UserInputState.Begin
				if flying then
					return Enum.ContextActionResult.Sink
				end
			end), false, 2001, playerAction
		end

		ContextActionService:BindActionAtPriority(logMovement("FlightForward", Enum.PlayerActions.CharacterForward))
		ContextActionService:BindActionAtPriority(logMovement("FlightBackward", Enum.PlayerActions.CharacterBackward))
		ContextActionService:BindActionAtPriority(logMovement("FlightLeft", Enum.PlayerActions.CharacterLeft))
		ContextActionService:BindActionAtPriority(logMovement("FlightRight", Enum.PlayerActions.CharacterRight))
		
		
		alignOrientation.Enabled = flying
		LinearVelocity.Enabled = flying
		ContextActionService:BindActionAtPriority(
			"ToggleFlight", 
			function(actionName, inputState, inputObject)
				if inputState == Enum.UserInputState.Begin then
					if not flying and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
						flying = true
						alignOrientation.Enabled = true
						LinearVelocity.Enabled = true
					else
						flying = false
						alignOrientation.Enabled = false
						LinearVelocity.Enabled = false
					end
				end
			end,
			false,
			2001,
			Enum.PlayerActions.CharacterJump
		)
		
		RunSerivce:BindToRenderStep("Fly", 1, function()
			local velocity = Vector3.new()
			local speed = 10
			
			if movementLog[Enum.PlayerActions.CharacterForward] then
				velocity += camera.CFrame.LookVector * speed
			end
			
			if movementLog[Enum.PlayerActions.CharacterBackward] then
				velocity += camera.CFrame.LookVector * -speed
			end
			
			if movementLog[Enum.PlayerActions.CharacterRight] then
				velocity += camera.CFrame.RightVector * speed
			end
			
			if movementLog[Enum.PlayerActions.CharacterLeft] then
				velocity += camera.CFrame.RightVector * -speed
			end
			
			LinearVelocity.VectorVelocity = velocity
			alignOrientation.CFrame = camera.CFrame.Rotation
		end)
		local connection
		connection = Humanoid.Died:Connect(function()
			disableFlight()
			connection:Disconnect()
		end)
	else
		disableFlight()
	end
end

task.wait(3)
canFly(true)


It does not really help me :(, I would like just to know how can I turn the character when I look with my mouse when I right click.

do you mean when you turn your camera?

No, I want to turn my character where i look with my mouse, imagine your character is looking always your mouse on your screen for example.

Something like this?

local Players = game:GetService("Players")
local RunSerivce = game:GetService("RunService")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()

local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
RunSerivce.RenderStepped:Connect(function()
	local pivot = character:GetPivot()
	character:PivotTo(CFrame.new(pivot.Position, mouse.hit.Position))
end)
1 Like

Just a last thing, I have a little problem with space and leftshilft keys, it does not stop for no reason, just I press one time and it was like if I was pressing for a lot of seconds.

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

local uis = game:GetService("UserInputService")

local Moving = false
local rightclicking = false


uis.InputBegan:Connect(function(input, process) 
	local char = plr.Character
	local isAble = plr.AdminTerm.isFlying.Value
	if input.KeyCode == Enum.KeyCode.W then 
		if isAble then 
			Moving = true
			while Moving  do
				char:PivotTo(char:GetPivot() * CFrame.new(0,0,-1))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.S then 
		if isAble then 
			Moving = true
			while Moving do
				char:PivotTo(char:GetPivot() * CFrame.new(0,0,1))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.A then 
		if isAble then 
			Moving = true
			while Moving do
				char:PivotTo(char:GetPivot() * CFrame.new(-1,0,0))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.D then 
		if isAble then 
			Moving = true
			while Moving do
				char:PivotTo(char:GetPivot() * CFrame.new(1,0,0))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.Space then 
		if isAble then 
			Moving = true
			while Moving do
				char:PivotTo(char:GetPivot() + Vector3.new(0,1,0))
				task.wait(0.01)
			end
		end
	elseif input.KeyCode == Enum.KeyCode.LeftShift then 
		if isAble then 
			Moving = true
			while Moving do
				char:PivotTo(char:GetPivot() + Vector3.new(0,-1,0))
				task.wait(0.01)
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, _gameProceesedEvent)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.S then
		local isAble = plr.AdminTerm.isFlying.Value
		if isAble and Moving then
			Moving = false
		end
	end
end)

mouse.Button2Up:Connect(function()
	rightclicking = false
end)

mouse.Button2Down:Connect(function()
	local isAble = plr.AdminTerm.isFlying.Value
	local char = plr.Character
	rightclicking = true
	if isAble then
		while rightclicking do 
			local pivot = char:GetPivot()
			char:PivotTo(CFrame.new(pivot.Position, mouse.hit.Position))
			task.wait(0.01)
		end
	end
end)

Wait no my bad I just forgot to add keys on InputEnded.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.