So I’m making a turret script and I want it so if they are in the group they don’t get shot does anyone know why it wont work.
turret.PrimaryPart = turret:WaitForChild("RotationPart")
local Player = game:GetService("Players")
local cooldown = 1
local cooldownActive = false
local maxRange = 50 -- range of how far
local dmg = 5 -- dmg of the player
while wait() do
local closestCharacter = nil
for i, plr in pairs(game.Players:GetPlayers()) do
if plr.Character then
local distance = (plr.Character.HumanoidRootPart.Position - turret.PrimaryPart.Position).Magnitude
if not closestCharacter or (closestCharacter.HumanoidRootPart.Position - turret.PrimaryPart.Position).Magnitude > distance then
local cf = CFrame.new(turret.PrimaryPart.Position, plr.Character.HumanoidRootPart.Position)
local ray = Ray.new(turret.PrimaryPart.Position, cf.LookVector * maxRange)
local part, position = workspace:FindPartOnRayWithIgnoreList(ray, turret.Parent:GetDescendants())
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if humanoid == plr.Character.Humanoid and distance <= maxRange and humanoid.Health > 0 then
closestCharacter = plr.Character
end
end
end
end
end
if Player:IsInGroup(3) then
else if closestCharacter then
turret:SetPrimaryPartCFrame(turret.PrimaryPart.CFrame:Lerp(CFrame.new(turret.PrimaryPart.Position, closestCharacter.HumanoidRootPart.Position), 0.3))
local humanoid = closestCharacter:FindFirstChild("Humanoid")
spawn(function()
if humanoid and not cooldownActive then
cooldownActive = false
humanoid:TakeDamage(dmg)
turret.ShootPart.ShootSound:Play()
local bullet = Instance.new("Part")
local distance = (turret.ShootPart.Position - closestCharacter.HumanoidRootPart.Position).Magnitude
bullet.CFrame = CFrame.new(turret.ShootPart.Position, closestCharacter.HumanoidRootPart.Position)
bullet.CFrame = bullet.CFrame + (bullet.CFrame.LookVector * distance / 2)
bullet.Size = Vector3.new(0.05, 0.05, distance)
bullet.CanCollide = false
bullet.Anchored = true
bullet.Color = Color3.fromRGB(239, 184, 56)
bullet.Parent = workspace
game.Debris:AddItem(bullet, 0.1)
wait(cooldown)
cooldownActive = false
end
end)
end
end
end```