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

How Would You Get a Player's Mouse Position and Use it Globally?

Asked by 5 years ago

Is there a way to constantly check for a player's mouse position (mouse.Hit.p) and utilize it within a non-local script? I have tried numerous different things, none of which seem to be working. Do I need to use remote functions or is there an easier way?

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Remote Functions is the way the to go,you could send mouse.Hit.p through the remote in a global script and use it on from there as a variable

Example:

Local Script

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()

game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit.p)

Server Script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,position)

-- The "position" variable is the mouse.Hit.p that you have sent through the RemoteEvent , now you can use it in any way, for example

print(position)
end)
Ad
Log in to vote
0
Answered by
oreoollie 649 Moderation Voter
5 years ago
Edited 5 years ago

Yea, the best way to pass the mouse target to the server would be remote events. I'm not really sure what you're trying to achieve with this but their are likely better ways to do this that don't involve giving the server the mouse target.

Nevertheless, here's how you would go about that.

Localscript:

local mouse = game:GetService("Players").LocalPlayer:GetMouse()

mouse:GetPropertyChangedSignal("Target"):Connect(function()
    MouseTargetEvent:FireServer(mouse.Hit.p)
end)

Server script:

MouseTargetEvent.OnServerEvent:Connect(MouseTargetChangedFunction)

If you feel my answer solved your problem and was the best please remember to accept it!

0
The LocalScript doesn't call the function for whatever reason. Like, the mouse target isn't changing or something... corncob567 275 — 5y

Answer this question