How would I use mouse in a server script?

How would I use this in a server script?

local tool = script.Parent
local rs = game.ReplicatedStorage

		if mouse.Target and mouse.Target.Parent then
			if(mouse.Target.Name == "Grass") then
				local Mpos = mouse.Hit.Position
				local NewHole = rs.BrownHole:Clone()
				newHole.PrimaryPart.Position = Mpos
				newHole.Parent = workspace


You don’t.
If you want the change to be visible on the server, you need to make a remote event to inform the server of the changes.

1 Like

You can fire a remote function to grab the player’s mouse. Obviously, you could check what’s returned for security reasons.

1 Like

I know I need to use a remote event, I just don’t know how to transfer mouse position, which is what I guess I should be asking.

Pass it as an argument in the remote event.


local mouse = RemoteFunction:InvokeClient()
-- security checks


RemoteFunction.OnClientInvoke = function()
   return game:GetService("Players").LocalPlayer.Mouse

You cannot pass objects as arguments, and even if you did this would still not work as the players mouse does not replicate to the server. All your code here would do is return nil.

You’re right for the second part since Mouse isn’t replicated to the server, but you can pass objects as arguments… sorry for the misleading information. Another alternative is only to pass other values I guess, such as the mouse position.

1 Like

I’d like to say it’s also probably never a good idea to use InvokeClient.
For something like this, a RemoteEvent’s FireServer would do just fine.

In response to OP, you would simply pass the property you want from the mouse as an argument when calling FireServer.
In this case you would put something along the lines of remoteEvent:FireServer(mouse.Hit.Position) in your Button1Down event and have the server handle the rest.


should be
return game:GetService("Players").LocalPlayer.Mouse.X, game:GetService("Players").LocalPlayer.Mouse.Y