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

Is there a way to send mouse target to sever?

Asked by 5 years ago

Is there a way to send mouse target to sever?

2 answers

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

0
remove event can also send it as an argument :p theking48989987 2147 — 5y
0
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. User#19524 175 — 5y
Ad
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

You need to use Local Script and Remote Event

Roblox Wiki Page - Local Script

Roblox Wiki Page - RemoteEvent

Roblox Wiki Page - Mouse

Roblox Wiki Page - Mouse.Target

Roblox Wiki Page - Mouse.Hit

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! :)

Answer this question