Why am I getting the error 12:12:34.561 - Touched is not a valid member of Model

I am trying to figure out ways to make a model look at whatever is touches a part, but I keep getting this error.12:12:34.561 - Touched is not a valid member of Model
Script for the minigun

local tool = script.Parent
local range = script.Parent.Parent.Stats.range.Value
local damage = script.Parent.Parent.Stats.damage
local Bullet = Instance.new("Part", workspace)
local ShootSpeed = script.Parent.Parent.Stats.shootspeed.Value
local Sound = script.Parent.Sound
local Radius = Instance.new("Part", workspace)
local TestEvent = game.ReplicatedStorage.Events.TestEvent
local ClosestEnemy = tool.Parent.Stats.ClosestEnemy
local Rotate = 3
Radius.CanCollide = true
Radius.Parent = tool
Radius.Position = tool.Parent["Left Leg"].Position
Radius.Position = Radius.Position + Vector3.new(-3,0.025,0)
Radius.CanCollide = false
Radius.Anchored = true
Radius.Size = Vector3.new(10,0,10)
Radius.Name = "MinigunRadius"
function shoot()
	Bullet.Transparency = 0
	Bullet.CanCollide = true
	Bullet.Position = script.Parent.Handle.Position
	Bullet.Anchored = true
	Bullet.Name = "MiniGun1Bullet"
	Bullet.BrickColor = BrickColor.new("New Yeller")
	Bullet.TopSurface = Enum.SurfaceType.Smooth
	Bullet.BottomSurface = Enum.SurfaceType.Smooth
	Bullet.Size = Vector3.new(0.3,0.1,0.1)
	Bullet.Orientation = Vector3.new(0, 90, 0)
	Bullet.Parent = tool
	for yes = 1,range do
		wait()
		local function closestEnemyFunction()
			ClosestEnemy.Value = tool.Parent:GetChildren(CFrame.Angles(ClosestEnemy.torso.Orientation))
		end
		TestEvent.Event:Connect(closestEnemyFunction)
		if Rotate == 1 then
			Bullet.Position = Bullet.Position + Vector3.new(0,0,-1)
		end
		if Rotate == 2 then
			Bullet.Position = Bullet.Position + Vector3.new(0,0,1)
		end
		if Rotate == 3 then
			Bullet.Position = Bullet.Position + Vector3.new(-1,0,0)
		end
		if Rotate == 4 then
			Bullet.Position = Bullet.Position + Vector3.new(1,0,0)
		end
	end
	Bullet.Transparency = 1
	Bullet.CanCollide = false
		end
while wait(ShootSpeed) do
	Sound:Play()
	shoot()
	end

Script for the zombie

FastZombie = script.Parent
local TestEvent = game.ReplicatedStorage.Events.TestEvent
function onTouched(hit)
	if (hit.Name == "MinigunRadius") then
		print('touched minigun radius')
		local NameValue = Instance.new("StringValue", game.Workspace.Minigunner.Stats)
		NameValue.Name = 'ClosestEnemy'
		NameValue.Value = FastZombie.Torso.Orientation
		TestEvent:Fire(workspace.Minigunner.Tazer.Script)
	   end
end
connection = FastZombie.Touched:connect(onTouched)
2 Likes

This error usually means that Touched does not exist in a Model. In this case, you calling Touched on the wrong thing. It has to be on a Part.

Instead of FastZombie.Touched, use something like FastZombie.HumanoidRootPart.Touched.

8 Likes

That stopped the error, but the minigunner is still standing still

local function closestEnemyFunction()
			ClosestEnemy.Value = tool.Parent:GetChildren(CFrame.Angles(ClosestEnemy.torso.Orientation))
		end
		TestEvent.Event:Connect(closestEnemyFunction)

and it doesn’t print ‘touched minigun radius’ in this

if (hit.Name == "MinigunRadius") then
		print('touched minigun radius')
		local NameValue = Instance.new("StringValue", game.Workspace.Minigunner.Stats)
1 Like

Yes he is right a model cannot be “touched” as the model isn’t actually a part itself it’s just a thing to group parts.

5 Likes

For the top script, not sure what you’re accomplishing there, but GetChildren doesn’t need any arguments, just :GetChildren() as is.

For the second script, the way hit works in a Touched function is that it returns the part that it detects touching. So for a player it can be a Left Arm or a Right Arm and so forth.

2 Likes

for the top script

local function closestEnemyFunction()
			
		end
TestEvent.Event:Connect(closestEnemyFunction)

I am trying to get the npc (minigunner) to look at the closest zombie (the one that touches the radius from this script) the problem is I have no clue how to do that, so I was trying to make the zombie create a string value when it touches the radius, and then the minigunner would look at the one that touched it.

FastZombie = script.Parent
local TestEvent = game.ReplicatedStorage.Events.TestEvent
function onTouched(hit)
	if (hit.Name == "MinigunRadius") then
		print('touched minigun radius')
		local NameValue = Instance.new("StringValue", game.Workspace.Minigunner.Stats)
		NameValue.Name = 'ClosestEnemy'
		NameValue.Value = FastZombie.Torso.Orientation
		TestEvent:Fire()
	   end
end
connection = FastZombie.HumanoidRootPart.Touched:connect(onTouched)
1 Like

This snippet of code means that if the touched brick is named “MinigunRadius”, which most of the time it won’t, then it will do the code below it. To detect if it is a zombie, first you need to check if the Parent of the part has a Humanoid (hit.Parent:FindFirstChild).

If it finds it, you can check if the zombie is indeed a zombie (maybe a boolvalue on them), if it is, it will fire the event, where it would focus on the target.

2 Likes

wait, but the part it is touching won’t have a humanoid?

1 Like

True, but if the part is part of a player or NPC, the Parent of it would contain the Humanoid
Something like this:

  • Zombie
    • Humanoid
    • Right Arm (Example if it touches the radius)

Using hit.Parent will check the actual model of the NPC to check if it has a Humanoid.

2 Likes

wait, so would I do something like this?

FastZombie = script.Parent
local TestEvent = game.ReplicatedStorage.Events.TestEvent
function onTouched(hit)
	if (hit.Parent.Parent.Name == "Minigunner") then
		print('touched minigun radius')
		local NameValue = Instance.new("StringValue", game.Workspace.Minigunner.Stats)
		NameValue.Name = 'ClosestEnemy'
		NameValue.Value = FastZombie.Torso.Orientation
		TestEvent:Fire()
	   end
end
connection = FastZombie.HumanoidRootPart.Touched:connect(onTouched)
1 Like

Almost, you don’t need the extra .Parent for the NPC, otherwise, depending where it’s located, it would come out as if it’s comparing Workspace to Minigunner (aka if "workspace" == "Minigunner then)

1 Like

Ok, so for the top script would it be something like this?
(Sorry for asking so many questions, I am still new to scripting and this is just a huge thing that is breaking the game)

local function closestEnemyFunction()
			local ClosestEnemy = tool.Parent.Stats.ClosestEnemy
			script.Parent.Parent.Torso.Orientation = ClosestEnemy.Orientation
		end
		TestEvent.Event:Connect(closestEnemyFunction)

That might not work since the orientation of the NPC would be the same as the target, making it seem like it’s facing away from the target. Also, from my experience, it would only rotate the torso.

Using this:

script.Parent.Parent.Torsot.CFrame = CFrame.new(script.Parent.Parent.Torso.Position, ClosestEnemy.Position * Vector3.new(1,0,1) + script.Parent.Parent.Torso.Position * Vector3.new(0,1,0))

Would rotate the NPC to face the target.

2nd argument is probably deprecated and I would’ve used .fromMatrix(), but it’s beyond my knowledge

2 Likes

the npc still just faces in the same area

Interesting, what is ClosestEnemy supposed to be, the model of a zombie?

yeah, it was supposed to change the value to the name of the zombie that was in the radius

If it is a model, that means we’ll use the Torso of the value (or HumanoidRootPart if it exists)

script.Parent.Parent.Torsot.CFrame = CFrame.new(script.Parent.Parent.Torso.Position, ClosestEnemy.Parent.Torso.Position * Vector3.new(1,0,1) + script.Parent.Parent.Torso.Position * Vector3.new(0,1,0))

still just stays in one place, I checked and it’s not anchored

I just realized I misspelled Torso. See if fixing the mistake would fix it.

Oh yea, top tip: enable “Output Window” in Studio to check for errors.

1 Like

I tried that, didn’t work still and there are no errors