Is there a way to get the object a local player's mouse hits in workspace?
1.) Target property
The mouse has a property called Target
, which is a part the player's mouse hits.
game:GetService('RunService').RenderStepped:Connect(function() print(player:GetMouse().Target ~= nil and player:GetMouse().Target.Name or 'no target') -- prints the name of the object the player's mouse hits or 'no target' if it doesn't hit anything end)
2.) Rays
This is pretty much how the Mouse.Target
property gets the part that hits the workspace. You need to shoot a ray from the Mouse's Origin(a CFrame positioned at the camera and oriented to where the player's mouse is).
local ray = Ray.new( Mouse.Origin.p, Mouse.UnitRay.Direction * 50 ) local hit = workspace:FindPartOnRay(ray, character) print(hit ~= nil and hit.Name or 'no part')
I know someone have answered, but I want to help you in the same way.
You can see of Mouse in Roblox Wiki > Roblox Wiki Page - Mouse
Also you can see Mouse.Target
in Roblox Wiki > Roblox Wiki Page - Mouse.Target
Also you can see Mouse.Button1Down
in Roblox Wiki > Roblox Wiki Page - Mouse.Button1Down
And you can see Mouse.Hit
in Roblox Wiki > Roblox Wiki Page - Mouse.Hit
You only can get Mouse in LocalScript, for get in Server, you need to use a Remote Events
For get position you can use Mouse.Hit.p For get object you need to use Mouse.Target
Local Script - player:GetMouse()
:
local player = game.Players.LocalPlayer local Mouse = player:GetMouse() -- you can use Mouse.Move > Mouse.Move:Connect(function() if Mouse.Target ~= nil and Mouse.Hit.p ~= nil then warn("Name: " .. tostring(Mouse.Target) .. "\nPosition: " .. tostring(Mouse.Hit.p)) else warn("The mouse target is nil!") end end) -- or you can use RunService > RenderStepped, but is not equal of Mouse.Move, RenderStepped is "basicaly a loop"! game:GetService("RunService").RenderStepped:Connect(function() if Mouse.Target ~= nil and Mouse.Hit.p ~= nil then warn("Name: " .. tostring(Mouse.Target) .. "\nPosition: " .. tostring(Mouse.Hit.p)) else warn("The mouse target is nil!") end end)
Or you can get Mouse with a Tool Local Script - Tool
:
-- You can get mouse in Tool.Equiped local player = game.Players.LocalPlayer local mouse = player:GetMouse() local Tool = script.Parent -- For use in Tool.Actived you need to use player:GetMouse() Tool.Activated:Connect(function() print(mouse) mouse.Move:Connect(function() if mouse.Target ~= nil and mouse.Hit.p ~= nil then warn("Name: " .. tostring(mouse.Target) .. "\nPosition: " .. tostring(mouse.Hit.p)) else warn("The mouse target is nil!") end end) -- or you can use RunService > RenderStepped, but is not equal of Mouse.Move, RenderStepped is "basicaly a loop"! game:GetService("RunService").RenderStepped:Connect(function() if mouse.Target ~= nil and mouse.Hit.p ~= nil then warn("Name: " .. tostring(mouse.Target) .. "\nPosition: " .. tostring(mouse.Hit.p)) else warn("The mouse target is nil!") end end) end) -- For use in Tool.Equiped you can use Tool.Equiped:Connect(function(MOUSE) Tool.Equipped:Connect(function(Mouse) -- you can use Mouse.Move > Mouse.Move:Connect(function() if Mouse.Target ~= nil and Mouse.Hit.p ~= nil then warn("Name: " .. tostring(Mouse.Target) .. "\nPosition: " .. tostring(Mouse.Hit.p)) else warn("The mouse target is nil!") end end) -- or you can use RunService > RenderStepped, but is not equal of Mouse.Move, RenderStepped is "basicaly a loop"! game:GetService("RunService").RenderStepped:Connect(function() if Mouse.Target ~= nil and Mouse.Hit.p ~= nil then warn("Name: " .. tostring(Mouse.Target) .. "\nPosition: " .. tostring(Mouse.Hit.p)) else warn("The mouse target is nil!") end end) end)
Get on server:
Create a RemoteEvent
put in ReplicatedStorage name as "MouseEvent" (You can change.)
Insert a script on ServerScriptService and put:
local event = game:GetService("ReplicatedStorage").MouseEvent -- Location of event -- You cannot get Mouse with Remote Event, but you can get positions. event.OnServerEvent:Connect(function(p,hit_pos,hit_target) -- Get player,hit pos, hit target if hit_pos ~= nil and hit_target ~= nil then -- If not nil then warn("Name: " .. tostring(hit_target) .. "\nPosition: " .. tostring(hit_pos)) -- Print name and position else warn("The mouse target is nil!") -- Print nil end end)
Now insert a LocalScript
and put it on StarterGui and put it:
local plr = game.Players.LocalPlayer -- Get player local Mouse = plr:GetMouse() -- Get player mouse local event = game:GetService("ReplicatedStorage").MouseEvent -- Event location -- you can use Mouse.Move Mouse.Move:Connect(function() event:FireServer(Mouse.Hit.p,Mouse.Target) end) -- or you can use RunService > RenderStepped, but is not equal of Mouse.Move, RenderStepped is "basicaly a loop"! game:GetService("RunService").RenderStepped:Connect(function() event:FireServer(Mouse.Hit.p,Mouse.Target) end)
For detect click you need to use Mouse.Button1Down
Example:
local plr = game.Players.LocalPlayer -- Get player local Mouse = plr:GetMouse() -- Get player mouse Mouse.Button1Down:Connect(function() if Mouse.Target ~= nil and Mouse.Hit.p ~= nil then warn("Name: " .. tostring(Mouse.Target) .. "\nPosition: " .. tostring(Mouse.Hit.p)) else warn("The mouse target is nil!") end end)