Why does my whitelisting script not work?

Hello, readers and kind strangers!
This script listed below is my script for a turret I’ve made. It’s very recent, and I am very new at Ray Casting and NPC Programming, so please, don’t judge too hard. I’d love to hear suggestions on how to improve.
Anyway… my problem:
In the while wait loop, it’s supposed to sort out if the name of the parent of the HumanoidRootPart is not the same as one of the members of my ,friendlyPlayers" array.
It seems as though my turret completely ignores that part and starts shooting me, although my name being in the list.

local Turret = script.Parent.Base
local Cannon = script.Parent.Cannon
local turretGroup = script.Parent
local debounce = 5
local range = 30 --studs
local damage = 10
local audio = script.Parent.shotSound
local lights = turretGroup.Lights
local players = game:GetService(“Players”)
local friendlyPlayers = {
“MLG10lucky_man”,
“Dummy”
}

while wait(debounce) do

local rayPosition = Cannon.Position
local rayDirection = Cannon.CFrame.LookVector * (range)
local rayParams = RaycastParams.new()

rayParams.FilterDescendantsInstances = {turretGroup:GetChildren()}
rayParams.FilterType = Enum.RaycastFilterType.Blacklist

local rayPart
local rayCast = workspace:Raycast(rayPosition, rayDirection, rayParams)

local worldParts = workspace:GetDescendants()


for i, value in pairs(worldParts) do
	
		if value:IsA("Humanoid") then

			local rootPart = value.Parent:WaitForChild("HumanoidRootPart")
			local parent = rootPart.Parent
			
			for index = 1, #friendlyPlayers do
			
			    local subject = friendlyPlayers[index]
			
				if parent.Name ~= subject then
											
					if rootPart and (rootPart.Position - Turret.Position).magnitude < range and rootPart.Parent.Humanoid.Health > 0 then

						for i, v in pairs(lights:GetChildren()) do
							v.Color = Color3.fromRGB(250,0,0)
						end

						Turret.CFrame = CFrame.new(Turret.Position, rootPart.Position)

						if rayCast then

							if rayCast.Instance then

								rayPart = rayCast.Instance

								if rayPart then

									local hum = rayPart.Parent:FindFirstChildWhichIsA("Humanoid") or rayPart.Parent.Parent:FindFirstChildWhichIsA("Humanoid")

									if hum then

										if hum.Health > 0 then
											audio:Play()
											hum.Health = hum.Health - damage
										end
									end
								end
							end
						end
					else
						for i, v in pairs(lights:GetChildren()) do
							v.Color = Color3.fromRGB(250,250,250)
						end
					end								
				end
			end
		end		
end	

end

2 Likes

It’s because you run through every subject when you already found yourself, meaning that when subject goes to "Dummy" it won’t recognize you anymore. You have to break the loop once parent.Name == subject or something like that. Also you can use Humanoid:TakeDamage(int) as opposed to just subtracting from the health.

1 Like

Thank you for your quick answer.
By breaking the for i loop when the same name was found and changing subject to the name that was found, I managed to make my turret only shoot those not whitelisted. I also learned that break exists in the process. Thank you once again!