Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Better way to make part face mouse constantly?

Asked by 4 years ago

I wanted to know if there was a better way to make a part face your mouse constantly other than:

Local Script

local player = game.Players.LocalPlayer;
local mouse = player:GetMouse();

local rs = game:GetService('ReplicatedStorage');
local re = rs:FindFirstChild('RemoteEvent') or rs:WaitForChild('RemoteEvent');
local rf = rs:FindFirstChild('RemoteFunction') or rs:WaitForChild('RemoteFunction');

mouse.Button1Down:connect(function()
    re:FireServer()
end)

rf.OnClientInvoke = function()
    return mouse.Hit.p
end

Script

local rs = game:GetService('ReplicatedStorage');
local re = rs:FindFirstChild('RemoteEvent') or rs:WaitForChild('RemoteEvent');
local rf = rs:FindFirstChild('RemoteFunction') or rs:WaitForChild('RemoteFunction');

re.OnServerEvent:connect(function(player)
    local part = Instance.new('Part', workspace);
    part.Anchored = true;
    part.CFrame = player.Character.Head.CFrame;

    while wait() do
        part.CFrame = CFrame.new(part.CFrame.p, rf:InvokeClient(player));
    end
end)

1 answer

Log in to vote
0
Answered by
Misqueso 145
4 years ago
Edited 4 years ago

You can make the loop go faster by using the RunService.

while game:GetService("RunService").Heartbeat:wait() do
    part.CFrame = CFrame.new(part.CFrame.p, rf:InvokeClient(player));
end

Heartbeat:wait() is faster than wait() as it waits 1/30 of a second.

If you want it to smoothly transition, you can use lerp to smoothly transition between positions. Assuming you still want to update it quickly:

while game:GetService("RunService").Heartbeat:wait() do
    part.CFrame = part.CFrame:lerp(CFrame.new(part.CFrame.p, rf:InvokeClient(player)), .1);  -- .1 controls its update speed.
end
1
Are you implying that this is the only way to accomplish the task and that this is the only improvement that can be made? When I ran it myself, it felt really laggy, so is there a better, less laggier way, or just this? fluffyoreos32 63 — 4y
0
You can also try to smoothly transition between the positions. There is no other way except by doing it locally. Misqueso 145 — 4y
Ad

Answer this question