I'm trying to make the character move towards their mouse position, but it lags. I figured out the reason for this lag is because I'm calling the MousePos remotefunction way too much. Is there a way that I can get the player's mouse position every time the loop repeats, without causing lag?

___________________________________________________________________________

Here is the loop:

function activateBroom(player, count, maxcount, mouse)
	repeat
		wait()
		local mousepos = remotes.Parent.ServerToClient.MousePos:InvokeClient(player)
		player.Character.HumanoidRootPart.CFrame = CFrame.new(player.Character.HumanoidRootPart.CFrame.p, mousepos.p) * CFrame.new(0, 0, -1)
		count = count + 1
	until count == maxcount
end

remotes.LearnedMoves.OnServerEvent:connect(activateBroom)
-- didnt bother to rename the remote event yet, i was using it for a different purpose before.

Here is the localscript that handles the remotes:

local remotes = game.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("ServerToClient")

function returnMousePos()
	return game.Players.LocalPlayer:GetMouse().Hit
end

remotes.MousePos.OnClientInvoke = returnMousePos

7 comments

0

Use the .Enabled argument of the tool.

0

what the heck are you talking about? there's no tool involved in this lmao

0

I'm not really sure what to do either, I asked the same question, but maybe try making a value for a certain player in repstore and making the client set the value to the position. Also if you're trying to make it a bit faster use RunService.RenderStepped:Wait().

0

maybe update it 30 or 20 times a second instead of 60

0

I tried RunService.RenderStepped but it only made it a bit smoother. i think it's because roblox limits the fps to 60?

0

It's a broom. Why wouldn't it be a tool?

0

maybe i don't want it to be a tool... maybe it's a magical flying broom lmao