"hit.parent:Isa" not working

I’m new to coding with Lua and haven’t learned much but I can’t figure out why this script won’t work.
The parent of the script is the NPC model and the script’s function is to kill the NPC when a car hits it.

local npc = script.Parent:FindFirstChild("Torso") -- object going to be hit
local humanoid = script.Parent:FindFirstChild("Humanoid") --Humanoid

function onTouched(hit)
	print("HIT! Object being touched:", hit.Name) -- This prints diiferent parts of "classic car" that hit the torso
		if hit.parent:Isa("classic car") then--double checking if the object's parent is the model "classic car"
		print("CAR HIT!")
		humanoid:TakeDamage(100) --Kill NPC when car touches it
	   end
	end


npc.Touched:Connect(onTouched) --When the torso is touched it connects to the function "OnTouched"

Any help would be appreciated!

Also, is it possible to re write the script to where the NPC dies when the car hits it at a certain velocity? Thank you!

IsA returns the class of an item if its true

1 Like

Luau is caps sensitive (literally all programming languages). Use IsA or isA.

1 Like

Everything is possible if you can think about it. Now I won’t spoonfeed because I don’t know how your game works, but Touched event and If statements are your friends here.

1 Like

I have fixed that error, however, the NPC still doesn’t lose any health. The parts of the car are touching the torso, yet nothing is happening.

Also try setting the humanoid’s health to 0 instead.
Humanoid.Health = 0

Both do the same thing. Except this is better if someone magically has 101 health.

Oh! IsA takes Class as a parameter, not an Instance.

if hit.Parent:IsA("Model") and hit.Parent.Name == "classic car" then
1 Like

Instance:IsA(str) checks if the ClassName of the Instance is the same as the string parameter, example:

local part = workspace.Part

if part:IsA("BasePart") then
    print("workspace.Part is a BasePart")
end

You can do that instead:

if hit.Parent.Name = "classic car" then
    ...
end
1 Like

I see where I went wrong! I have also tried the code, however, I said hit.Parent == "classic car"
Thank you all for the help!

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