Is there a way to send mouse target to sever?
COMMUNITY QUESTION
Is there a way to send mouse target to sever?
2 answers
RemoteFunctions
--Server script in serverscriptservice
local rf = game.ReplicatedStorage.RemoteFunction
-- If you want to invoke the RemoteFunction, use the `:Invoke` method. You must also include a player as the first argument
-- Example:
-- rf:InvokeClient(game.Players.Doobb_yyus)
--Client script in StarterPack, StarterCharacterScripts, or StarterPlayerScripts
local rf = game.ReplicatedStorage.RemoteFunction
function rf.OnClientInvoke()
return Mouse.Target
end
Note: if the parent wasn't created on the server, it'll be nil on the server
You need to use Local Script and Remote Event
Roblox Wiki Page - Local Script
Roblox Wiki Page - RemoteEvent
Roblox Wiki Page - Mouse.Target
Roblox Wiki Page - Mouse.Button1Down
You can see Roblox Wiki for more information.
First insert a RemoteEvent on ReplicatedStorage, insert a Script on ServerScriptService and put this code:
local event = game:GetService("ReplicatedStorage").RemoteEvent -- Location of event!
event.OnServerEvent:Connect(function(p,hit_target)
if hit_target ~= nil then
warn(tostring(hit_target))
else
warn("The mouse target is nil!")
end
end)
Now insert a LocalScript on StarterGui and put his code:
repeat wait() until game.Players.LocalPlayer
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local event = game:GetService("ReplicatedStorage").RemoteEvent -- Location of event.
Mouse.Button1Down:Connect(function()
event:FireServer(Mouse.Target)
end)
If you want to get position you can use this:
Script on ServerScriptService:
local event = game:GetService("ReplicatedStorage").RemoteEvent -- Location of event
event.OnServerEvent:Connect(function(p,hit_pos,hit_target)
if hit_target ~= nil and hit_pos ~= nil then
warn("Name: " .. tostring(hit_target) .. "\nPosition: " .. tostring(hit_pos))
else
warn("The mouse target is nil!")
end
end)
LocalScript on StarterGui:
repeat wait() until game.Players.LocalPlayer
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local event = game:GetService("ReplicatedStorage").RemoteEvent -- Location of event
Mouse.Button1Down:Connect(function()
event:FireServer(Mouse.Hit.p,Mouse.Target)
end)
Hope it helped! :)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
remove event can also send it as an argument :p
This is a bad way. A client could just not return and have the server yield. Remote events server sided as suggested above would be better.