How do I get the position that the mouse is pointing on the screen in a script that is on the server side??
You can’t really get the mouse position from server. You want the client to send that information through remote, because everyone’s mouse position is most likely unique.
Use RemoteEvents
to transfer over the data in the parameters
You can use a RemoteFunction to get information from the Client to the server via invoking, if you just need that only, that’s how I would do that
You would also need to specify which player since there would be multiple players on one server
but every time the script that is on the server side is executed will this event be triggered several times?
What do you want to accomplish? You want to constantly get the player’s mouse’s position, or just once when the player clicks?
just once when the player clicks
If the script somehow decided to use a global variable, it would collide. If it runs within its own scope, no problem. The event should be triggered several times, but I doubt it’ll cross the limit.
Here’s an example of how I did it.
local function mouseButtonClick(actionName, inputState, inputObject)
if actionName == "ClickObject" then
if inputState == Enum.UserInputState.Begin then
print("The mouse's 2d position = " .. "x: " .. inputObject.Position.X .. ", y: " .. inputObject.Position.Y)
--elseif inputState == Enum.UserInputState.End then
--
end
end
end
ContextActionService:BindAction("ClickObject", mouseButtonClick, false, Enum.UserInputType.MouseButton1)
Make sure this is in a local script.
You would want to then set up a remote event to a server script so that the server can receive what the player’s mouse’s position is.
.
Another question: Are you trying to get the 3d point from the mouse? Or the 2d point?
you need use remote event and send the mouse information from the client to the server
-- Server
game.ReplicatedStorage.Event.OnServerEvent:Connect(function(player, mouse)
print(mouse)
end)
-- Client
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
game.ReplicatedStorage.Event:FireServer(mouse.Hit.Position)
this works but i’m making a script that waits for a while, and in the meantime the player has certainly changed the position of your mouse
Dk why you’re waiting but
--Client Side
local Player = game.Players.LocalPLayer
local Mouse = Player:GetMouse()
local MouseEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
wait(30) --Unless if you want the event to wait on the server side
MouseEvent.FireServer(Mouse.Hit.Position)
--Server Side
local MouseEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
MouseEvent.OnServerEvent:Connect(function(Player, MousePos)
print("Mouse's position "..MousePos)
end)
If you use my method, you will get the player’s mouse position each time they click the left mouse button. When the player clicks down on the button, it will fire the server, sending the x, y coordinates to the server. You can set up a debounce as such:
-- Local script
local ContextActionService = game:GetService("ContextActionService")
local debounce = false
local function waitTime()
wait(2)
debounce = false
end
local function mouseButtonClick(actionName, inputState, inputObject)
if actionName == "ClickObject" then
if inputState == Enum.UserInputState.Begin then
if not debounce then
debounce = true
spawn(waitTime)
print("The mouse's 2d position = " .. "x: " .. inputObject.Position.X .. ", y: " .. inputObject.Position.Y)
remoteEvent:FireServer(inputObject.Position)
end
end
end
end
ContextActionService:BindAction("ClickObject", mouseButtonClick, false, Enum.UserInputType.MouseButton1)
I’m guessing you want to get the 3d point from the mouse, no?
You will need to cast a ray. I use Camera:ViewportPointToRay(x, y, 0). This will take the mouse’s x, y position and convert it to a unit ray.
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {--Objects you want the ray to ignore or only hit}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist -- This causes the specified object(s) to be ignored by the ray
local length = 50 -- This will set the length of the ray in studs
Then you will need to create a ray, using local newRay = Ray.new(unitRay.Origin, unitRay.Direction * length, raycastParams)
and finally, you must cast it, like a fishing rod jk. But it must be cast now. We can do this by: local raycastResult = game.Workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
Now you can see a bunch of different results from the ray that’s been cast from your mouse’s position. But for this example, we will get the 3d world point from where the ray hit, thus giving us the 3d point where the player clicked. We can retrieve this from raycastResult
.
print(raycastResult.Position) -- Getting the position where the ray hit
.
Final script:
-- Local script
local ContextActionService = game:GetService("ContextActionService")
local remoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local debounce = false
local function waitTime()
wait(2)
debounce = false
end
local function mouseButtonClick(actionName, inputState, inputObject)
if actionName == "ClickObject" then
if inputState == Enum.UserInputState.Begin then
if not debounce then
debounce = true
spawn(waitTime)
print("The mouse's 2d position = " .. "x: " .. inputObject.Position.X .. ", y: " .. inputObject.Position.Y)
local length = 50
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {--Object to put into blacklist}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local unitRay = camera:ScreenPointToRay(inputObject.Position.X, inputObject.Position.Y, 0)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * length) -- a unit ray is 1 stud long, so we just multiply for how much length in studs we want the ray to be
-- Returns results based on the ray being cast
local raycastResult = game.Workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
remoteEvent:FireServer(raycastResult.Position) -- Sending the Vector3 coordinates from where the ray hit a surface/object.
end
end
end
end
ContextActionService:BindAction("ClickObject", mouseButtonClick, false, Enum.UserInputType.MouseButton1)
-- Server script
local remoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
remoteEvent.OnServerEvent:Connect(function(position)
-- Whatever it is you want to do with the position.
print("x = " .. position.X .. ", y = " .. position.Y .. ", z = " .. position.Z)
end)