Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Better way to make part face mouse constantly?

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

✓ Accepted answer

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

2 comments

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?

0

You can also try to smoothly transition between the positions. There is no other way except by doing it locally.

This discussion is preserved as it was. New answers, comments, and votes are closed.