Heya,
What would be the best way to detect if a player, who has a specific tool equipped, has hit an object (Part) with their tool?
Essentially, I want to detect when a player clicks their mouse on an object while they have a tool equipped. I need to be able to detect what player executed this action, what tool they've got equipped and what part have they hit.
I'm developing with Filtering Enabled. I'm aware of ClickDetectors but I don't think I can detect a MouseClick through it if player has a tool equipped?
I've just began developing with Roblox Studio, so I'm still familiarising with concepts.
Help would be appreciated!
Right, so with a little bit more of research I was able to figure out how to get the result I desired.
Firstly, I detect when my desired tool is equipped and then "Connect" a function that introduces UserInputService - which detects when the player presses Left Mouse Button down.
local function onEquip() toolEquiped = true UserInputService.InputBegan:Connect(function(InputObject) if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then pickaxeHit() end end) end local function onUnequip() toolEquiped = false end tool.Equipped:Connect(onEquip) tool.Unequipped:Connect(onUnequip)
The further function Hit() then checks for the player's mouse Target, checks if it's a suitable Target and how far away from the Target the player is. Then, if player is close enough to the Target, they qualify for "hitting" an object.
function pickaxeHit() if toolEquiped then local Track = humanoid:LoadAnimation(anim_pickaxe_hit) Track:Play() local mouse = player:GetMouse() local target = mouse.Target if target.Name == "Stone Normal" then if player:DistanceFromCharacter(target.Position) < 6.0 then print "Award rock" end end end end
Hope this helps anyone that has the same question on how to accomplish this.