I nead help to detect model

Hello guys!!

I am making a gun system and I got my first problem.

So the problem is that I want when the player clicks on a part that is in a model to print the model name instead of the part name!!

Here the code:

local eq = false
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
script.Parent.Equipped:Connect(function()
	eq = true
end)
script.Parent.Unequipped:Connect(function()
	
		eq = false

end)

mouse.Button1Down:Connect(function()
	if eq == true then
		local Target1 = mouse.Target
		if Target1.ClassName == 'Part' then
			print(Target1.Parent.Parent)
		end
	end
	
end)

Thank you in advance!!

antolol7350

1 Like

Pretty sure it’s because you’re using

if Target1.ClassName == 'Part' then

try changing it to

if Target1.ClassName == 'Model' then

because with the original code you’re pretty much looking for a part, not a model, then change the
print to print(Target1)

hope this helps

1 Like

i don’t think this will work because if i only do print(Target1) this will print a part and not a model

it should print the model because Target1 is defined as a model, but in your script it’s defined as a Part. Give it a try and let me know

I tried it does not work because the target of the mouse is a part

If you are trying to find the model that the part is inside of, just do:

Target1.Parent.Name

I hope this helps :slight_smile:

Yeah but for example if it’s an accesory the parent of the part is the accesory like We have the accesory handle and afther the Accesory

1 Like

Then, you need to find the first model up, so you do this:

if Target1.Parent.ClassName == 'Model' then
 --Code Here
elseif Target1.Parent.Parent.ClassName == 'Model' then
 --Code Here
else
 print('No Model')
end

Hope this helps :slight_smile:

This might Actually work but i can’t test right now Because i am on mobile (if it work tommorow am gonna put this as the solution)

1 Like

That’s fine, if you need any more help, just tell me.

1 Like

A big thank you because it worked so thank you!!

1 Like

Happy to help if you need anymore help. :slight_smile: