Mouse.target for gun doesnt work when clicked on accessory

Hey, I’m trying to make a simple gun system with mouse.target, but whenever I clicked on any accessory it just doesn’t work. I it’s because I need to add another .Parent to it(bc mouse.target = the handle of the accessory), but even doing so it doesn’t work. Can someone help me with this?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local Dmg = 1



tool.Activated:Connect(function()
	if mouse.Target == nil then
		return
	else
		if mouse.Target.Parent:IsA("Player") or mouse.Target.Parent:IsA("Model") then
			local hum = mouse.Target.Parent:FindFirstChild("Humanoid")
			if hum == nil then
				return
			else
				if mouse.Target.Parent:IsA("Accessory") then
					print("clicked on accessory")
					local hum = mouse.Target.Parent.Parent:FindFirstChild("Humanoid")
					hum.Health = hum.Health -Dmg
				end
				print(hum)
				hum.Health = hum.Health -Dmg
			end
			
		end
	end
end)

This is all in a local script
PS: I know I should make humanoids take damage in a local script, because it just doesn’t work, but for now I’m just testing. I will let a humanoid take damage with a bindable event later on.

Moving

local mouse = player:GetMouse()

into the tool.Activated event worked for me. I’m pretty sure that the properties of the Mouse class don’t change by themselves and you need to call the function every time you want an update.

1 Like

Clicking any accessory still doesnt do anything.

This should work:

tool.Activated:Connect(function()
	local mouse = player:GetMouse()
	if mouse.Target == nil then
		return
	else
		local hum = mouse.Target.Parent:FindFirstChild("Humanoid")
		if not hum and mouse.Target.Parent:IsA("Accessory") then
			hum = mouse.Target.Parent.Parent:FindFirstChild("Humanoid")
		end
		if hum then hum.Health -= Dmg end
	end
end)

Replace your tool.Activated handler with this one.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.