I prefer to use my own sorta ClickDetector system w/ the use of UserInputService.InputBegan & Camera::ScreenPointToRay.
Now you’ll want to Connect on InputBegan firstly, of course this is in a LocalScript:
--// LocalScript, StarterPlayerScripts
local UserInputService = game:GetService("UserInputService");
local Players = game:GetService("Players");
local Player = Players.LocalPlayer; --// Needed to access the LocalPlayer's Character
local Camera = workspace.CurrentCamera;
local ToolName = "Tool"; --// Input your tool name
local function InputBegan(Input, Processed)
--// Code
end
UserInputService.InputBegan:Connect(InputBegan);
Now inside the function you’ll want to check if the UserInputTypeInput is MouseButton1 (or other UserInputTypes).
Additionally, that it isn’t Processed (meaning that they might be typing in a TextBox or doing some other UI action) and that their character exists with the Tool equipped.
if (Input.UserInputType == Enum.UserInputType.MouseButton1 and not Processed) then
if (LocalPlayer.Character and LocalPlayer.Character:FindFirstChild(ToolName)) then --// Split into two statements for less longer lines
--// Code
end
end
Finally, you’ll want to use ScreenPointToRay w/ the Input.Position however this returned a unit Ray (a Ray which is one in length) and must be extended by firing a longer Ray from the same Origin but with the direction multiplied.
Then you can use FindPartOnRay (or another Ray method) and complete what you’d like to achieve.
local UnitRay = Camera:ScreenPointToRay(Input.Position.X, Input.Position.Y);
local ExtendedRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 5000);
local HitPart = workspace:FindPartOnRay(ExtendedRay, LocalPlayer.Character) --// Ignore the LocalPlayer's Character
if (HitPart) then
--// Do whatever you'd like with the Part!
end
Create a Server Script and put it inside of the ClickDetector
local clickDectector = script.Parent
clickDetector.MouseClick:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local tool = player.Character:FindFirstChild("TOOL_NAME_HERE")
if(tool) then
--player clicked the model with a certain tool equipped
--make something happen here
end)
end)
local clickDectector = script.Parent
clickDetector.MouseClick:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local tool = player.Character:FindFirstChild("TOOL_NAME_HERE")
if tool then
--player clicked the model with a certain tool equipped
--make something happen here
end
end)
Would be a fixation to his code, however this wouldn’t be a good method due to the fact its unreliable. I would recommend using @ReturnedTrue 's method as that way you can place it within the tool and get reliable ray casts. You’d then just bind a click function to check whats currently being hovered over - if its a descendant of any of the button model -> do your thing.