Hello there!
I am trying to make a Character’s HumanoidRootPart face the Cursor’s Position or CFrame.
I only have done it by the Client but i canot give the Mouse by a remote event to another client because it says nill instead of saying the Mouse, The Code:
while wait() do
local player = game:GetService("Players").LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = player:GetMouse()
Character.HumanoidRootPart.CFrame = CFrame.lookAt(Character.HumanoidRootPart.Position, Mouse.Hit.p)
end
If you want all of the players to face your cursor, then you’d have to FireServer() and from the server FireAllClients(), however that would be really laggy when used in your loop.
Also I recommend using RunService.RenderStepped and not while wait() do.
i meant to give the Mouse variable to the remote so the server has the Mouse variable, then the server will give that Mouse variabe to all the clients and the clients whould loop Mouse.Hit.p on the Character that is sending those Client Event, beacause if i loop Mouse.Hit.p on the server, it would lag. But my problem is that When you send the Mouse Variable to the Server or the Client, the mouse variable whould be nill and it will give errors when i use Mouse.Hit.p
Well, first of all you should do some protecting then, since if every player that will join the game will run this script then all players will just be using each others mouse positions and it will get super messy. Second of all you could do :FireServer(Mouse.Hit.p), you said it throws an error, could you send what error it throws?
You can’t send the mouse object itself as an argument, only its properties. Are you wanting every player to face your mouse on only your client or on the server?
I use on a local scipt this code when you hold down the button e it does once this:
LockToMouseEvent:FireServer("Start", Mouse, Character)
--Start is the event i named so it starts to lock the player on the mouse, when i want to stop i add Stop instead of Start.
When the remote fire to the Server it does this:
LockToMouseEvent.OnServerEvent:Connect(function(player, Event, Mouse, eCharacter)
local Character = player.Character or player.CharacterAdded:Wait()
if Event == "Start" then
for i, v in pairs(game.Workspace:GetDescendants()) do
if v.Name == "HumanoidRootPart" and game.Players:FindFirstChild(v.Parent.Name) then
local Magnitude = (v.Position - Character.HumanoidRootPart.Position).Magnitude
if Magnitude <= 1000 then
LockToMouseEvent:FireClient(game.Players:FindFirstChild(v.Parent.Name), "Start", Mouse, eCharacter)
end
end
end
elseif Event == "Stop" then
for i, v in pairs(game.Workspace:GetDescendants()) do
if v.Name == "HumanoidRootPart" and game.Players:FindFirstChild(v.Parent.Name) then
local Magnitude = (v.Position - Character.HumanoidRootPart.Position).Magnitude
if Magnitude <= 1000 then
LockToMouseEvent:FireClient(game.Players:FindFirstChild(v.Parent.Name), "Stop", Mouse, eCharacter)
end
end
end
end
end)
-- It basicly finds all the players that are 1000 blocks away from the player that Holded e and then it will fire a remote event to theire client so it shows to them in there the movement of the Character that pressed E.
In theire client it runs this:
local LockToMouseEvent = game.ReplicatedStorage.CombatSystem.LockToMouse
local Lock = false
LockToMouseEvent.OnClientEvent:Connect(function(Event, Mouse, eCharacter)
if Event == "Start" then
Lock = true
while wait() do
if Lock == true then
eCharacter.HumanoidRootPart.CFrame = CFrame.lookAt(eCharacter.HumanoidRootPart, Mouse.Hit.p)
end
end
elseif Event == "Stop" then
Lock = false
LockToMouseEvent:FireServer("LockServer", Mouse, eCharacter)
end
end)
But then i whould need to add it on a loop and it whould send alot of massages on the server and it whould lag, thats one of my problems that i cannot find how to solve
Character replicates to the server, so if you set the character looking to the cursor on client, everyone will see you are facing your cursor. No need to :FireServer().