Hello! I’m in progress of making my sentry and it’s ALMOST finished; however, there are some bugs that I don’t know how to fix? I have code that’s not functioning properly but more specifically the part where it checks if the Target_Player is == nil
local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local Sentry = script.Parent
local Sentry_Head = Sentry:WaitForChild("Sentry_Head")
local Sentry_SFX = Sentry:WaitForChild("Sentry_SFX")
local Sentry_Fire = Sentry_SFX:WaitForChild("Sentry_Fire")
local Sentry_Idle = Sentry_SFX:WaitForChild("Sentry_Idle")
local Sentry_Target_Spotted = Sentry_SFX:WaitForChild("Sentry_Target_Spotted")
local Sentry_Guns = Sentry:WaitForChild("Sentry_Guns")
local Sentry_Gun_Holder = Sentry_Guns:WaitForChild("Sentry_Gun_Holder")
local Ray_Origin = Sentry_Gun_Holder:WaitForChild("Ray_Origin")
local Sentry_Module = require(Sentry:WaitForChild("Sentry_Module"))
local MAX_RANGE = 100
local COOLDOWN = 0.1
local ROTATE_TIME = 0.2
local Idle = true
local Idle_Function_IS_RUNNING = true -- To Prevent Rotating Task Spawn Function from being called multiple times
local Shooting = false
local function Visualizer(Origin, End_Position, Distance, LASER_COLOR)
local Laser = Instance.new("Part")
Laser.CanCollide = false
Laser.CanTouch = false
Laser.CanQuery = false
Laser.CastShadow = false
Laser.Anchored = true
Laser.Color = LASER_COLOR
Laser.Material = Enum.Material.Neon
Laser.Size = Vector3.new(0.1, 0.1, Distance)
Laser.CFrame = CFrame.lookAt(Origin, End_Position) * CFrame.new(Vector3.new(0, 0, -Distance/2))
Laser.Parent = workspace
Debris:AddItem(Laser, 0.1)
end
local function Get_Nearest_Player()
local Magnitudes = {}
local Target_Player = nil
for i, player in Players:GetPlayers() do
if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then continue end
local character = player.Character or player.CharacterAdded:Wait()
local HRP = character:FindFirstChild("HumanoidRootPart")
local Distance = player:DistanceFromCharacter(Sentry_Head.Position)
if Distance > MAX_RANGE then continue end
table.insert(Magnitudes, {Distance, player})
end
table.sort(Magnitudes, function(a, b)
return a[1] < b[2]
end)
if #Magnitudes > 0 then
Target_Player = Magnitudes[1][2]
else
return nil
end
print("TARGET: ".. Target_Player.Name)
return Target_Player
end
local Idle_Rotation = task.spawn(function()
while Idle == true do
task.spawn(Sentry_Module.Play_Sentry_SFX, Sentry_Idle)
for i = 0, 4, 1 do
local Random_Rotation = math.random(-45, 45)
local Rotate_Sentry_Head = TweenService:Create(Sentry_Head, TweenInfo.new(1, Enum.EasingStyle.Linear), {CFrame = Sentry_Head.CFrame * CFrame.Angles(0, math.rad(Random_Rotation), 0)})
Rotate_Sentry_Head:Play()
Rotate_Sentry_Head.Completed:Wait()
task.wait(ROTATE_TIME)
end
end
end)
while true do
local Target_Player = Get_Nearest_Player()
if Target_Player == nil then
print(Idle)
print(Idle_Function_IS_RUNNING)
if Idle == true then return end
Idle = true
if Idle_Function_IS_RUNNING == true then return end
Idle_Function_IS_RUNNING = true
task.spawn(Idle_Rotation)
else
Idle = false
Idle_Function_IS_RUNNING = false
task.cancel(Idle_Rotation)
if not Target_Player.Character or not Target_Player.Character:FindFirstChild("HumanoidRootPart") then
Idle = true
Idle_Function_IS_RUNNING = true
task.spawn(Idle_Rotation)
return
end
local Target_Character = Target_Player.Character or Target_Player.CharacterAdded:Wait()
local Target_HRP = Target_Character:FindFirstChild("HumanoidRootPart")
local Origin = Ray_Origin.WorldCFrame.Position
local Direction = Target_HRP.Position - Origin
local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParameters.FilterDescendantsInstances = {Sentry}
RaycastParameters.IgnoreWater = true
local RayResults = workspace:Raycast(Origin, Direction, RaycastParameters)
if not RayResults then
print("Hit Nothing!")
else
local Hit_Instance = RayResults.Instance
local Hit_Position = RayResults.Position
local Hit_Distance = RayResults.Distance
print("Hit: ".. RayResults.Instance.Name)
if Hit_Instance.Name == "Handle" and Hit_Instance.Parent:IsA("Accessory") and Players:GetPlayerFromCharacter(Hit_Instance.Parent.Parent) or Players:GetPlayerFromCharacter(Hit_Instance.Parent) then
task.spawn(function()
if Shooting == true then return end
Shooting = true
task.spawn(Sentry_Module.Play_Sentry_SFX, Sentry_Fire)
end)
local LookAt_CFrame = CFrame.lookAt(Sentry_Head.Position, Hit_Position)
task.spawn(Sentry_Module.Play_Sentry_SFX, Sentry_Fire)
task.spawn(Visualizer, Origin, Hit_Position, Hit_Distance, Color3.fromRGB(0, 255, 0))
TweenService:Create(Sentry_Head, TweenInfo.new(0.25, Enum.EasingStyle.Linear), {CFrame = LookAt_CFrame}):Play()
else
Shooting = false
Idle = true
Idle_Function_IS_RUNNING = true
task.spawn(Idle_Rotation)
end
end
end
task.wait(COOLDOWN)
end
Whenever I remove the checks if Target_Player is nil, it manages to find the player; however, when I add it, it stays in idle. Here are two screenshots that compares both with and without. The first screenshot is the one with and the second screenshot is the one without.

