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

Best way to get a players mouse x,y,z?

Asked by 4 years ago

I have a localscript thats gives me a players mouse coordinates but I want to be able to give that information to the server using remotevents or functions.

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Question

Best way to get a players mouse x,y,z?

Given Context

You have a local script that gives you the mouse coordinates.

Solution

You are right. You will use remote events, in this case, to send the coordinates to the server. (Note you cannot send the mouse directly, you have to send the position instead)

Simply create a Script (Not a LocalScript) in ServerScriptService and create a remote event that will have it's OnServerEvent signal handled by that script.

Inside the LocalScript you will :FireServer with the mouse's position and the server will retrieve it.

Example Codeblock

LocalScript

--// LocalScript

local players = game:GetService("Players")

local client = players.LocalPlayer
local mouse = client:GetMouse()

local remoteEvent = ... --// define reference to your remote event

remoteEvent:FireServer(mouse.Hit.Position) --// send the server the position

ServerScript

--// Server Script

local remoteEvent = ... --// define reference to your remote event

local function handlePosition(player, position) --// player who fired is automatically passed. 
    print(player.Name, "'s mouse position is: ", position)
end

remoteEvent.OnServerEvent:Connect(handlePosition)

Further Reading

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

0
Thanks! ComradeREKTangle 39 — 4y
Ad

Answer this question