Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Solved] How to detect if player hit an object with specific tool equipped?

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!

3 comments

0

be more clear, ur saying 2 things at once as in "hit an object" or "clicked an object"

0

it doesn't make sense that you'd need to detect what tool they had equipped since you only want to detect a specific tool?

0

My point was that whether an object is clicked or it's been "hit" doesn't matter as in both cases I would be able to perform my function. Maybe I could have worded better, but I either click or hit detection is suitable for me.

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.