Cannot Anchor and Rotate Player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I need to make player able to maintain a x,y,z position, aka stop in mid air, while being able to turn their player humanoidrootpart on the x,y,z axis. It must replicate to the server.

  1. What is the issue? Include screenshots / videos if possible!
    – float
    function MovementController:Anchor()
    HumanoidRootPart.Anchored = true
    end

– stop float
function MovementController:Unanchor()
HumanoidRootPart.Anchored = false
end

-----Makes the HumanoidRootPart face the mouse
function MovementController:PointPlayerToMouse()
if Connection then
Connection:Disconnect()
end
local Root = HumanoidRootPart
local Mouse = Player:GetMouse()
Connection = Mouse.Move:Connect(function()
Root.CFrame = CFrame.new(Root.Position, Root.Position + Mouse.Hit.LookVector)
end)
end

My code does not replicate to the server. The server does replicate the player being anchored, but not the client side rotations. Player also glitches around.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    velocity = Instance.new(“BodyVelocity”)
    velocity.Parent = Player.Character.HumanoidRootPart
    velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    velocity.P = 300
    velocity.Velocity = Vector3.new(0, 0, 0)
    I tried using a max force velovity, and even a body position. They made the player anchor, but I could not rotate them. I tried using a BodyGyro to rotate them, but if I use a MaxForce Velocity, it will negate that.
2 Likes

Just anchor them as you have done, and then whilst they’re in the air do something like this:

MovementController.Replicate = game:GetService("ReplicatedStorage"):WaitForChild "FloatToggle"
MovementController.isFloating = false
MovementController.autoRotatePreset

function MovementController:ToggleFloat()
	local Character = Player.Character
	if not Character or not Character:IsDescendantOf(game.Workspace) or not Character:FindFirstChild "Humanoid" or Character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end
	if MovementController.isFloating then
		MovementController.isFloating = true
		Character.HumanoidRootPart.Anchored = false
		MovementController.autoRotatePreset = Character.Humanoid.AutoRotate
		
		local Mouse = Player:GetMouse()
		local conn; conn game:GetService("RunService").RenderStepped:conect(function (delta)
			if MovementController.isFloating then
				if Player.Character then
					local Character = Player.Character
					if not Character or not Character:IsDescendantOf(game.Workspace) or not Character:FindFirstChild "Humanoid" or Character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then
						return conn:disconnect()
					end
					Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.CFrame.p, Mouse.Hit.p)
				else
					return conn:disconnect()
				end
			else
				return conn:disconnect()
			end
		end)
		
		--> Replicate the anchor
		MovementController.Replicate:FireServer(true)
	else
		MovementController.isFloating = false
		Character.HumanoidRootPart.Anchored = false
		Character.Humanoid.AutoRotate = MovementController.autoRotatePreset
		
		--> Replicate the unanchor
		MovementController.Replicate:FireServer(false)
	end
end

To use this then:

MovementController:ToggleFloat() --> Toggles on/off

In order to replicate this to the server you would need the following servercode:

local Event = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage")
Event.Name = "FloatToggle"

Event.OnServerEvent:connect(function (Player, Toggle)
	local Character = Player.Character
	if not Character or not Character:IsDescendantOf(game.Workspace) or not Character:FindFirstChild "Humanoid" or Character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end
	if type(Toggle) == "boolean" then
		Character.HumanoidRootPart.Anchored = Toggle
	end
end)
1 Like

BodyMovers. More specifically, body gyro. Never anchor your character. It’s bad practice. To replicate, you can create the body mover on the server, and send update or changes via a remote event from the client.

1 Like

There is no need to anchor the player’s character, in fact, anchoring prevents replication, as it prevents physics simulation on the part and changes on the client will not be replicated, as anchoring changes the network owner to “Auto”

If you just change the HumanoidRootPart’s CFrame in a Heartbeat event, that’ll work fine:

local rs = game:GetService("RunService")

Connecttion = rs.Heartbeat:Connect(function()
    Root.CFrame = CFrame.new(Root.Position, Root.Position + Mouse.Hit.lookVector)
end)

@sanjay2003 The player automatically has network ownership of their character, so physics calculated on the client will be replicated to the server. So, you don’t need to create the BodyMover on the server and use a RemoteEvent - you can make it on the client with no hassle

6 Likes

Thank you so much. I have been stuck on this for days.

1 Like