Character face toward mouse in server script

  1. I want the character to face the mouse on all axis
    for a bit of context I am making an ability that makes the player float in air and fires a bunch of bullets.
    2, the bullet creating script in server, and character rotating script is client, so the server doesn’t know the character is rotating, but i want the bullets to spawn in front of the character humanoidrootpart
    3.i did see this post
    Problem with torso following mouse server-sided
    and i did not know what the only answer was talking about, i know what setnetworkownership is but does not help

edit:
https://i.gyazo.com/1de61df62011568ef4e4373fda715a59.mp4

local script

RS.RenderStepped:Connect(function()
	if LookAtMouse then
		local MousePosition = mouse.Hit.Position
		Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position,Vector3.new(MousePosition.X, MousePosition.Y, MousePosition.Z))
	end
end)

srver script

	local HitboxPart = game.ReplicatedStorage.VisualEffects["Light Barrage"].Hitbox:Clone()
		HitboxPart.Parent = workspace.VFX
		HitboxPart.CanCollide = false
		HitboxPart.CFrame = player.Character.HumanoidRootPart.CFrame +player.Character.HumanoidRootPart.CFrame.LookVector *5
		HitboxPart:SetNetworkOwner(nil)
		--HitboxPart.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position - mouseposition)
		local PartVelocity = Instance.new("BodyVelocity")
		PartVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		PartVelocity.Velocity = (mouseposition.Position-HitboxPart.Position).Unit * BulletSpeed
		PartVelocity.Parent = HitboxPart

updated solved answer: fire a remote event inside Run Service’s RenderStepped, 10 times a second, and use Tween on server.
edit:

Can anyone please help me :cry: :cry: (30char)

local Char = (reference your character here)
local MousePosition = (can be reference by Player:GetMouse().Hit.Position in a local script)

while task.wait() do
	if (ability is enabled) then
		Char:SetPrimaryPartCFrame(CFrame.lookAt(Char.PrimaryPart.Position, MousePosition))
	end
end

Let me know if this works. You can use the CFrame.lookAt property to make a part face another position.

Could you just change it to be this?

RS.RenderStepped:Connect(function()
	if LookAtMouse then
		local MousePosition = mouse.Hit.Position
		Character.HumanoidRootPart.CFrame = CFrame.lookAt(Character.HumanoidRootPart.Position, MousePosition)
	end
end)
1 Like

I changed it to that but it’s still appearing
https://i.gyazo.com/1de61df62011568ef4e4373fda715a59.mp4
You can see the bullet is spawning in the same place no matter where my humanoidrootpart is facing

Could you not just set the starting position of the bullet to your HumanoidRootPart every time the bullet fires?

That’s what I did, in the server, like i said in the post the rotating script is a local script, so the server does not know when it is rotating, so other players cannot see my humanoidrootpart rotating neither

1 Like

Hmm. Try using remote events. Remote events can only be fired 20 times a second, however. You’re in a tricky situation.

Off the top of my head, I think the easiest way to solve your issue would be to have 4 remote events, and cycle through each one.

Fire the first one, task.wait(0.05), Fire the second remote event, task.wait(0.05), Fire the third remote event, etc. The more remote events you use, the less choppy the updating should look.

1 Like

This might not be the most efficient way, but I’ve seen others recommend this system, and I can’t think of anything else off the top of my head

I’m going to make a post about this, because this is something that I want to know at some point, if it’s not the most efficient way.

Edit 2:

For whatever reason, it says online that a remote event can only be fired 20 times a second (which is not true), so you would only need 1 remote event.

(If you see this, please send a reply. Devforum doesn’t allow me to send more than 3 replies in a row)

1 Like

but wouldn’t that make the server laggy? it’s firing the remote every frame to the server, sorry for the late reply

It wouldn’t make things laggy, since it’s still under 1 event. Think of it like this:

A server script detects a .Touched event 60 times a second. This won’t lag the game, since it’s all being detected by 1 event.

Firing one remote event 60 times a second is no different.

1 Like

Sorry, it’s a little difficult to explain haha. I keep confusing remote events with script events (Part.Touched:Connect stuff)

Could you not use shift lock?
Something like this

local function ToggleLock()
	local CameraController = cameras.activeCameraController
	local MouseLockController = cameras.activeMouseLockController
	CenterLocked = not CenterLocked
	if CenterLocked then
		if MouseLockController:GetIsMouseLocked() then -- toggle shift lock off
			MouseLockController:OnMouseLockToggled()
		end
		CameraController:SetMouseLockOffset(Vector3.new())
		CameraController:SetIsMouseLocked(true)
		BoundKeys.Value = "" -- disables shift lock toggle
	else
		CameraController:SetIsMouseLocked(false)
		BoundKeys.Value = OldBoundKeys -- restores shift lock toggle
	end
end

That is also a good idea which I didn’t think of. The issue with shift lock is that your mouse would have to be locked to the middle of your screen.

The video attached is what I would assume they want their system to function like.

1 Like

You should probably use CharacterRootPosition and spawn the bullets at that vector
You would have to get the vector first of course

HumanoidRootPart* -------------

Yes, that’s what we were trying to do. I recommended remote events to bring the information from the client to the server.

Maybe but it is it spawning at the torso or is that the problem

@Smartmunkee please read this post. The issue is that the player is only being rotated on their screen, since it’s in a local script. And the bullets are not spawning at the same place as the player.