How do I get the player's mouse position, not onscreen, but where it reaches a brick?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to achieve a system where it moves a brick to the location of my mouse.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t clearly know what to use.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Looked for solutions, and they only used screen mouse x and y. I tried using that, but it didn’t work. Could you help me, please? I need this for my game.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

And also, the mouse part is included in a model, which has the player’s name saved.

--Server
local clickRemote = game.ReplicatedStorage.BRICK
clickRemote.OnServerEvent:Connect(function(Player, Hit)
while task.wait() do
game.Workspace.MyBrick.Position = Hit
end
end)
--Client
local clickRemote = game.ReplicatedStorage.BRICK
while task.wait() do
clickRemote:FireServer(game.Players.LocalPlayer:GetMouse().Hit.Position)

end

This just… won’t work for me. I have the part, stored in a model, which will be cloned each time a player joins, and it has the mouse part inside. The model acts as a player, and it moves towards the mouse part.

Can you send your error logs?

Using mouse.Hit will get the position of the mouse in 3d space. Example:

local part --The part you want to move
local player = game.Player.LocalPlayer
local mouse = player:GetMouse()

part.Position = mouse.Hit.Position -- Hit will return a cframe, so you will have to use Hit.Position

This of course will only work in a local script, and therefor only for the client, if you want the part to move for all the players you will have to use a remote event.

Well that would work. I can replace the LocalPlayer value with player name, i have it stored in the player model.

Ok, I’m not sure if you can use GetMouse in a server script though (I can’t check right now I’m on mobile), but let me know if it works. If it doesn’t you’ll have to use a remote event somehow.

Yeah, doesn’t seem to work, and I don’t understand how remote events work, could you explain?

Here’s a solution I found that seems relevant to what you need…
https://devforum.roblox.com/t/part-position-mouse-location/1126174


Remote Events allow Local Scripts and Server Scripts to communicate with each other. You can read more about them here.


In a Local Script, game.Players.LocalPlayer already refers to the client’s player object, so you don’t need to change their name.


The player’s mouse can only be retrieved in a Local Script.


@JustAGameDeveloper1’s code is on the right lines, but I would rewrite the code using UserInputService (although how the code is written depends on what you want to do specifically), like so:

-- client code
local UIS = game:GetService("UserInputService")
local player = game.Player.LocalPlayer
local mouse = player:GetMouse()
local brickEvent = game:GetService("ReplicatedStorage").BRICK

UIS.InputChanged:Connect(function(input, gameprocessed)
  if input.UserInputType == Enum.UserInputType.MouseMovement then
    brickEvent:FireServer(mouse.Hit.Position)
  end
end)
-- server code
local brickEvent = game:GetService("ReplicatedStorage").BRICK

brickEvent.OnServerEvent:Connect(function(player, mousePosition)
    game.Workspace.MyBrick.Position = mousePosition
end)

Also, the Server and Client code must be in a Server and Local script respectively. Please explain how any code you try doesn’t work, and share any errors that appear.


Here is an example of a Remote Event in use with explanations.

-- this code is in a Local Script

-- the remote event is in `ReplicatedStorage`
local RepStorage = game:GetService("ReplicatedStorage")
local remoteEvent = RepStorage.RemoteEvent

-- sending a message over to the server side of the event
local message = "This is a message from the client!"
remoteEvent:FireServer(message)
-- this code is in a Server Script

-- the remote event also needs to be acquired from 
-- ReplicatedStorage in this script, just as it was in the Local Script

local RepStorage = game:GetService("ReplicatedStorage")
local remoteEvent = RepStorage.RemoteEvent -- the same event

-- two arguments get passed through the function which came
-- from the remote event...

-- * player: this is the player who sent the message, not their
             name but the ACTUAL player object, and this is the argument
             which always appears first before any other argument

-- * message: this is the message which was passed on from
              the event when called by the client 

local function onServerConnect(player, message)
    print(player.Name.." sent a message: "..message)
end

-- fires the onServerConnect function whenever a signal
-- is received from the client (local) side of the event
remoteEvent.OnServerEvent:Connect(onServerConnect)

This is a setup you could have:

  • the Remote Event is in game > ReplicatedStorage
  • the Server Script is in game > ServerScriptService
  • the Local Script is in game > StarterPlayer > StarterPlayerScripts

Screenshot 2022-11-26 at 16.34.55

1 Like

Assuming you know about client and server: Basically a remote event is like a “door” between the client and the server (put your remote events in replicated storage, that way both the client and server can access them), and you can pass information between them.

For example you have a TextButton that you want to make destroy a part, but you want the part destroyed for everyone, not just the player who pressed the button, so this is what you would do: You put a local script in the button, a script in ServerScriptService, and a remote event in ReplicatedStorage. Here’s the local script:

local button = script.Parent
local part = — The part to destroy
script.Parent.Activated:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer(part) — Firing the remote event with our information. Here you can send any arguments you want, just remember the order of the arguments when you’re receiving them.
end)

Server script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, partToDestroy) — Running a function whenever the remote event is fired. Roblox automatically sends a player argument when you fire a remote event to the server, so we have to put that first, then we receive the “part” argument.
    print(player.Name..” has called Destroy on “..partToDestroy.Name)
    partToDestroy:Destroy()
end)

I did my best to explain this, I hope it makes sense, but if it doesn’t there are plenty of YouTube videos with explanations and examples. Have a great day!

Edit: The commenting out seems to be doing something weird, I’m not quite sure how to fix it.

1 Like