How to see where the player clicked

So, im making a gun system inspired in Isle game, and for it i need to see what part the player clicked in workspace, but idk how to make this

Could anyone help me?

The script

local tool = script.Parent
local shoot_part = tool:WaitForChild("FirePart")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position)
	local origin = shoot_part.Position
	local player = game.Players.LocalPlayer

	player:GetMouse()
	--???
end)
local tool = script.Parent
local shoot_part = tool:WaitForChild("FirePart")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position)
	local origin = shoot_part.Position
	local player = game.Players.LocalPlayer

	local mouse = player:GetMouse()
	local Location = mouse.hit.p
end)

use the mouse.hit.p function to find out the pos

For Drawing Raycast

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast

local tool = script.Parent
local shoot_part = tool:WaitForChild("FirePart")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position)
	local origin = shoot_part.Position
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	mouse.Button1Down:Connect(function()
		local mousePos = mouse.Hit.p
		
	end)
end)

To get the mouse position whenever the local player left clicks.

1 Like

You can’t use GetMouse on a server script, and you can’t get the local player on the server as well. You’ll have to change it to look a bit like this:

--Client (Local script)
--Do whatever blah blah blah
--local mouse = player:GetMouse() --Replace player with the LocalPlayer/The variable containing local player
--fire the remote event with this (mouse.Target,mouse.Hit.p (optional), etc.)

--Server (Server script, normal script)
remote.OnServerEvent:Connect(function(player, target, position)
	local origin = shoot_part.Position
     --The target is the part that the mouse is targeting :)
end)
1 Like

Oh thx! i changed it for a test to print the part’s name and it worked!

1 Like

Oh yeah i got that from corn’s script

Ohh i also needed that lol, thx

Oh thx! this script also worked like corn’s script, but a lil bit simpler!

Oh thx! a lot of people replied here lol, also its a local script

No problem, you can also switch between mouse.Hit & mouse.Hit.p, the first will return a CFrame value and the second a Vector3 value, it all depends on what you prefer to use/find easier.

oh thx i didnt knew about that!

https://developer.roblox.com/en-us/api-reference/class/Mouse

Here you can take a look at all of the properties, functions & events which are tied to the mouse.

local tool = script.Parent
local shoot_part = tool:WaitForChild("FirePart")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position)
	local origin = shoot_part.Position
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	local targetPart = mouse.Target
	print(targetPart.Name)
	--perform some code here
end)

Modifying my script from earlier, the “Target” property will get the part instance in the workspace which is currently being targeted by the mouse.

yoooo tysm! i didnt knew there was that aand these are perfect for the system i want to achieve!