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

How would I go around making a model point towards the player's mouse?

Currently, I am using this really inefficient and laggy method.

--Script

script.Parent.Remotes.UpdatePos.OnServerInvoke = function(player, Model, pos1, pos2)
    Model:SetPrimaryPartCFrame(CFrame.new(pos1, pos2))
end

--

--LocalScript

Functions = require(script:WaitForChild("Functions"))
RbxUtility = LoadLibrary("RbxUtility")
local RunService = game:GetService("RunService")

local Character
local Player
local PlayerMouse
local Tool = script.Parent
Players = game:GetService("Players")

Remotes = Tool:WaitForChild("Remotes")
ClientControl = Remotes:WaitForChild("ClientControl")

local Handle = script.Parent:WaitForChild("Firer").Value

script.Parent.Equipped:connect(function()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	PlayerMouse = Player:GetMouse()
	local PreviousMouseHitP
	
	RunService.RenderStepped:connect(function()
	local magnitude = (Character.Torso.Position - PlayerMouse.Hit.p).magnitude
	if magnitude > 10 then
		if PlayerMouse.Hit.p == PreviousMouseHitP then
			
		else
			Remotes.UpdatePos:InvokeServer(Handle.Parent, Handle.Parent.PrimaryPart.Position, PlayerMouse.Hit.p)
			PreviousMouseHitP = PlayerMouse.Hit.p
		end
	end
end)
end)

--wait(.5)

It works but is, of course, not ideal as I'm invoking a RemoteFunction every render frame. Is there any way other that I could do this? Thanks.

1 answer

Firstly, a RemoteFunction is completely unnecessary here. Prefer RemoteEvents always, and use RemoteFunctions sparingly, if ever.

You should merely do this client-side. If you want to replicate this to other players, you can do so with remotes, but it's imperative that you give immediate visual feedback to a client.

Additionally, LoadLibrary is deprecated, you shouldn't use it. RBXScriptSignal:connect() is also deprecated, you should prefer RBXScriptSignal:Connect().

To define the player and character in your LocalScript, just do this:

local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

RBXScriptSignal:Wait() waits for the event to be fired and returns the arguments it was fired with.

Additionally, you are registering multiple event connections every time the tool is equipped, and never disconnecting them.

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