Part script not working

Im trying to do a script that when a player touches a door they go trought it. But if a zombie npc touches it, it dies. But the script does not work (the zombie that touches the part, doesnt die). If you could help me, I would appreciate it. Thanks


debounce = false

script.Parent.Touched:connect(function(hit)
	if (hit) then
		if (hit.Parent) then
			a = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if (a) then
				if (a.TeamColor == BrickColor.new("Institutional white")) then
					debounce = true
					script.Parent.Transparency = 0.5
					script.Parent.CanCollide = false
					wait(1)
					script.Parent.Transparency = 0
					script.Parent.CanCollide = true
					debounce = false
					
				else
					
					
					hit.Parent.Humanoid.Health = 0
					
					
				end
			end
		end
	end
end)
4 Likes

Hi, you error is that you didnt do local a = …

debounce = false

script.Parent.Touched:connect(function(hit)
	if hit then
		if hit.Parent then
			local a = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if a then
				if a.TeamColor == BrickColor.new("Institutional white") then
					debounce = true
					script.Parent.Transparency = 0.5
					script.Parent.CanCollide = false
					wait(1)
					script.Parent.Transparency = 0
					script.Parent.CanCollide = true
					debounce = false

				else


					hit.Parent.Humanoid.Health = 0


				end
			end
		end
	end
end)
2 Likes

youre trying to get the player from a character, a zombie npc is not a real player in the game so its gonna return nil

1 Like

oh yh that is so true :() lol why didnt i see that.

1 Like

i made it so players can go trough and it works. But when a zombie npc touches it, it doesnt die.

yes but the zombie isnt a player here is the corrected code.

debounce = false

script.Parent.Touched:connect(function(hit)
	if hit then
		if hit.Parent then
			local a = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if not a and a ~= nil then

				hit.Parent.Humanoid.Health = 0
			else
				
				if a.TeamColor == BrickColor.new("Institutional white") then
					debounce = true
					script.Parent.Transparency = 0.5
					script.Parent.CanCollide = false
					wait(1)
					script.Parent.Transparency = 0
					script.Parent.CanCollide = true
					debounce = false



				end
			end
		end
	end
end)
1 Like

because the zombie is not a real player, :GetPlayerFromCharacter finds the player that the character belongs to in game:GetService("Players") but the zombie doesnt have an actual player instance

2 Likes

i get this error when the zombie touches the door
16:06:30.028 Workspace.Human Door.Script2:7: attempt to index nil with ‘FindFirstChild’ - Server - Script2:7

are you using my code or your current code?

1 Like

im using your code (char limit)

Yeah exactly what the guy did. He looks if there is a player instance attached to the entity touching the door, if it find something the door is opened, ELSE it sets the humanoids Health to 0. There’s no error there clearly.

1 Like

Ok i have fixed the issue here is the fixed code (hopefully.)

1 Like

@dav2777 just a short question why do you bracket every if statement?

1 Like

Thats what i thought Aswell i just removed them.

1 Like

i dont know really (char limit)

Did the new code work if not try again because it seems i forgot to copy the code and i pasted the old code.

1 Like

16:12:43.102 Workspace.Human Door.Script2:12: attempt to index nil with ‘TeamColor’ - Server - Script2:12

hmh try this it returns end when hit is = to nil

debounce = false

script.Parent.Touched:connect(function(hit)
	if hit == nil then return end
	if hit then
		if hit.Parent then
			local a = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if not a and a ~= nil then

				hit.Parent.Humanoid.Health = 0
			else
				
				if a.TeamColor == BrickColor.new("Institutional white") then
					debounce = true
					script.Parent.Transparency = 0.5
					script.Parent.CanCollide = false
					wait(1)
					script.Parent.Transparency = 0
					script.Parent.CanCollide = true
					debounce = false



				end
			end
		end
	end
end)
1 Like

Correct code should be:

debounce = false

script.Parent.Touched:connect(function(hit)
	if hit then
		if hit.Parent then
			local a = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if a then
				if a.TeamColor == BrickColor.new("Institutional white") then
					debounce = true
					script.Parent.Transparency = 0.5
					script.Parent.CanCollide = false
					wait(1)
					script.Parent.Transparency = 0
					script.Parent.CanCollide = true
					debounce = false
				end
            elseif not a and hit.Parent:FindFirstChild("Humanoid")				
					hit.Parent.Humanoid.Health = 0	
			end
		end
	end
end)
2 Likes

im not sure that would work as it is in the team color if statment not the a if statment.

1 Like