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

Passing mouse through remote events?

Asked by
0hsa 193
4 years ago

I am trying to make a building system, but I need to get the players mouse through a remote event and nothing is working

example:

local script:

1local re = replicatedstorage:waitForChild("RemoteEvent",10)
2 
3local player  = game.Players.LocalPlayer
4local mouse = player:getMouse()
5 
6re:fireServer(mouse)

server script:

1local re = replicatedstorage:waitForChild("RemoteEvent",10)
2 
3local function event(player,mouse)
4    print(mouse.Hit.p) -- prints: attempt to index nil with 'Hit'
5end
6 
7re.OnServerEvent:connect(event)
0
The Mouse is a Client-controlled Object. You cannot read it from the Server. Ziffixture 6913 — 4y
0
what alternatives could i use? 0hsa 193 — 4y
0
You can't pass the mouse itself. Instead, just pass mouse.Hit.p from the client and then print it out on the server. xInfinityBear 1777 — 4y
0
i would, but i need the x,y,z coordinates along with the targetface, and target itself. 0hsa 193 — 4y

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago
Edited 4 years ago

The Mouse Object is becoming obsolete i would recode it using the UserInputService

But for now this is a way on how to solve the problem you're facing at the moment....

Code:

Server Script

01local replicatedstorage = game:GetService("ReplicatedStorage")
02local re = replicatedstorage:waitForChild("RemoteEvent",10)
03local Mouse = {
04    Wheel = 0,
05    Button1Clicked = false,
06    Button2Clicked = false,
07    Position = Vector2.new(),
08    Target = nil,
09    Hit = nil
10}
11local MouseFired = false
12local function event(player,mouse)
13 
14    for k,v in pairs(mouse) do
15        Mouse[k] = v
View all 33 lines...

Local script

01local replicatedstorage = game:GetService("ReplicatedStorage")
02local re = replicatedstorage:waitForChild("RemoteEvent",10)
03 
04local player  = game.Players.LocalPlayer
05local mouse = player:getMouse()
06local MouseWheelZeroTime = 0.01
07local Mouse = {
08    Wheel = 0,
09    Button1Clicked = false,
10    Button2Clicked = false,
11    Position = Vector2.new(),
12    Target = nil,
13    Hit = nil
14}
15 
View all 57 lines...

So in your Server script just remember you're working from the Mouse Table and not the Mouse object! As you cannot pass the mouse object from a local script as you know. So the above example is Side-stepping that problem and re-creating the Mouse object on both Client and server scripts through the use of a table in both. The only other problem i can foresee is if many people are playing the game you may get Exceeded number of callback Errors and scripts breaking due to the amount of data being sent.

To reduce the amount of data being sent through the Client Server Boundary I would limit the amount of calls to the server from the client. For instance if you're placing a block and wanted it to show on the screen at the mouse cursor point i would replicate or fake it in the client script and only when you place it down via Mousebutton1Click, send the updated data to the server. And if you wanted others to see it I would only update that once per second or something.

That way it drastically reduces the amount of calls from the client.

Just some ideas

Hope this helps! :)

For reference on the UserInputService you can check out the wiki here: https://developer.roblox.com/en-us/api-reference/class/UserInputService

0
cool idea. ill try that later, thanks ;D 0hsa 193 — 4y
Ad

Answer this question