Is this an ok way to get an up to date position of the players mouse? [Solved]

I need a way for my remote events to get the players mouse position 5 seconds AFTER the event is fired. Obviously using the value that comes with the event will be 5 seconds old so I came up with a new method.

I plan to have a local script that constantly fires an event which sends mouse.Hit to the server which is then stored in a CFrame value. Obviously it will be delayed if you are lagging but it will be half as delayed compared to sending a remote function invoke to the client which then has to get sent back. I can’t really see any other way to achieve this but I am worried that this has the potential to cause lag with the event being fired so often. Here are some demo scripts that are not made properly for multiplayer yet but they show the idea.

Client :

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	game.ReplicatedStorage.PingEvent:FireServer(game.Players.LocalPlayer:GetMouse().Hit)
end)

Server:

game.ReplicatedStorage.PingEvent.OnServerEvent:Connect(function(plr,Hit)
	script.MouseCFrame.Value = Hit
end)

The concept seems fine. I’d suggest looking into UnreliableRemoteEvent | Documentation - Roblox Creator Hub. It was designed for situations like this:

UnreliableRemoteEvent is best used for ephemeral events including effects that are only relevant for a short time, or for replicating continuously changing data.

I haven’t used it yet personally but it should reduce the lag that you’d see if you were using a reliable remote event.

1 Like

This seems perfect for this situation. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.