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

Getting The Player's Mouse With A Server Script?

Asked by 6 years ago

I want to be able to detect when (and where) a player clicks with his mouse, and summon a part there. To summon the part it needs to be a server script, but to get the mouse it needs to be a local script. I've heard I need to use RemoteEvents or RemoteFunctions but honestly I've looked into it and it really confuses me. I need to (from the server) ask a LocalScript to return the mouse variable it grabs, and then use that said variable. Since I'm asking from the server and expecting something back would it be a RemoteFunction? If so, how would I ask it to return the variable "mouse" and be able to use it in the server script? If I use a RemoteEvent, how would I go about doing that? Thanks to any responses. ^-^

0
Although Kiriots answer is great! It is worth pointing out that there are articles about how to stop exploiters from abusing code like that. If use correctly Remote F/Es can be very useful. Bellyrium 310 — 6y

1 answer

Log in to vote
2
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

You can't pass the local Mouse object to the server. However you can pass the position it points at.

(Note that the example below is completly insecure and you should add additional security to protect against exploiters)

Server script:

local remoteEv = Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
remoteEv.Name = "remoteEv"

local remoteCommands = {
    ['SpawnPart'] = function(pos)
        Instance.new("Part", workspace).CFrame = pos
    end,
}

remoteEv.OnServerEvent:Connect(function(plr, ...)
    local args = {...}
    local func = remoteCommands[args[1]]
    if func then
        pcall(func, select(2, ...))
    end
end)

LocalScript:

local plr = game:GetService"Players".LocalPlayer
local remoteEv = game:GetService("ReplicatedStorage"):WaitForChild("remoteEv")
local m = plr:GetMouse()

m.Button1Down:Connect(function() --note that you should use UserInputService for this purpose but for the sake of making it easier for you, I'm going to use Button1Down of Mouse
    remoteEv:FireServer("SpawnPart", CFrame.new(m.Hit.p)) --remove orientation
end)
Ad

Answer this question