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:
local re = replicatedstorage:waitForChild("RemoteEvent",10) local player = game.Players.LocalPlayer local mouse = player:getMouse() re:fireServer(mouse)
server script:
local re = replicatedstorage:waitForChild("RemoteEvent",10) local function event(player,mouse) print(mouse.Hit.p) -- prints: attempt to index nil with 'Hit' end re.OnServerEvent:connect(event)
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
local replicatedstorage = game:GetService("ReplicatedStorage") local re = replicatedstorage:waitForChild("RemoteEvent",10) local Mouse = { Wheel = 0, Button1Clicked = false, Button2Clicked = false, Position = Vector2.new(), Target = nil, Hit = nil } local MouseFired = false local function event(player,mouse) for k,v in pairs(mouse) do Mouse[k] = v end MouseFired = true end re.OnServerEvent:connect(event) while true do if(MouseFired == true) then MouseFired = false print("Mouse: ") table.foreach(Mouse,function(a,b) print("a ",a," = ",b) end) end wait() end
Local script
local replicatedstorage = game:GetService("ReplicatedStorage") local re = replicatedstorage:waitForChild("RemoteEvent",10) local player = game.Players.LocalPlayer local mouse = player:getMouse() local MouseWheelZeroTime = 0.01 local Mouse = { Wheel = 0, Button1Clicked = false, Button2Clicked = false, Position = Vector2.new(), Target = nil, Hit = nil } mouse.WheelBackward:connect(function() Mouse.Wheel = -1 re:fireServer(Mouse) wait(MouseWheelZeroTime) Mouse.Wheel = 0 re:fireServer(Mouse) end) mouse.WheelForward:connect(function() Mouse.Wheel = 1 re:fireServer(Mouse) wait(MouseWheelZeroTime) Mouse.Wheel = 0 re:fireServer(Mouse) end) mouse.Button1Up:connect(function() Mouse.Button1Clicked = false Mouse.Target = mouse.Target Mouse.Hit = mouse.Hit.Position re:fireServer(Mouse) end) mouse.Button1Down:connect(function() Mouse.Button1Clicked = true re:fireServer(Mouse) end) mouse.Button2Up:connect(function() Mouse.Button2Clicked = false re:fireServer(Mouse) end) mouse.Button2Down:connect(function() Mouse.Button2Clicked = true re:fireServer(Mouse) end) mouse.Move:connect(function(thing) Mouse.Position = Vector2.new(mouse.X,mouse.Y) re:fireServer(Mouse) end)
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