Basically I want to make a tool that when the player clicks something, it will print the name of whatever the player clicked, but I have no idea where to start.
Like as a UI object or in the workspace?
Look into localplayer:GetMouse() if you mean getting what part a player touched in the workspace.
tool activated connection, then use the players mouse and get the Target of it
local Players = game:GetService("Players");
local LocalPlayer = Players.LocalPlayer;
local Mouse = LocalPlayer:GetMouse()
Tool.Activated:Connect(function()
print(Mouse.Target.Name)
end)
This is purely an example based off of what you said, and is not clean
If you want just it, use what @Ihaveash0rtname just said.
If, in your game, you need something more, like “I don’t want to print the name of certain parts”, then you should do:
local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local tool = player:WaitForChild("Backpack"):WaitForChild("Tool")
local UnraycastableParts = workspace:WaitForChild("InvisibleWalls") -- only a example
local ClickRayParams = RaycastParams.new()
ClickRayParams.FilterType = Enum.RaycastFilterType.Exclude
ClickRayParams.FilterDescendantsInstances = {UnraycastableParts}
local function CastRayFromMouse(params)
local mousePosition = UIS:GetMouseLocation()
local ray = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000, params)
return result
end
tool.Activated:Connect(function()
local result = CastRayFromMouse(ClickRayParams)
if result and result.Instance then
print(result.Instance.Name)
else
print("Clicked on empty space")
end
end)
It is a bigger code but it’s better than LocalPlayer:GetMouse()
because you can customize more easily what you want or not want to be findable in your raycast.