Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

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!

1 comment

0

The LocalScript doesn't call the function for whatever reason. Like, the mouse target isn't changing or something...

This discussion is preserved as it was. New answers, comments, and votes are closed.