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 Point_Light = Ray_Origin:WaitForChild("PointLight")
local Sentry_Module = require(Sentry:WaitForChild("Sentry_Module"))
local Idle = true
local Target_Detected = false
local MAX_RANGE = 100
local COOLDOWN = 0.1
local ROTATE_TIME = 0.2
local v101 = Vector3.new(1, 0, 1)
local function Fire_Flash()
Point_Light.Enabled = true
task.wait(0.1)
Point_Light.Enabled = false
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 return nil end
Target_Player = Magnitudes[1][2]
return Target_Player
end
local Idle_Rotation = task.spawn(function()
while true do
if Idle == false then task.wait() continue end
task.spawn(Sentry_Module.Play_Sentry_SFX, Sentry_Idle)
for i = 0, 4, 1 do
if Idle == false then break end
local Random_Rotation = math.random(-45, 45)
local Current_Sentry_Head_CFrame = Sentry_Head.CFrame
local New_Sentry_Head_CFrame = CFrame.fromMatrix(Current_Sentry_Head_CFrame.Position, (Current_Sentry_Head_CFrame.RightVector*v101).Unit, (-(Current_Sentry_Head_CFrame.LookVector*v101).Unit:Cross((Current_Sentry_Head_CFrame.RightVector*v101).Unit)).Unit, -(Current_Sentry_Head_CFrame.LookVector*v101).Unit)
-- New_Sentry_Head_CFrame wasn't written by me, I asked someone for help on it.
local Rotate_Sentry_Head = TweenService:Create(Sentry_Head, TweenInfo.new(1, Enum.EasingStyle.Linear), {CFrame = New_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
if Idle == false then
Idle = true
end
if Target_Detected == true then
Target_Detected = false
end
else
repeat task.wait() until Target_Player.Character or Target_Player.CharacterAdded:Wait()
if not Target_Player.Character or not Target_Player.Character:FindFirstChild("HumanoidRootPart") then return end
local Target_Character = Target_Player.Character or Target_Player.CharacterAdded:Wait()
local Target_HRP = Target_Character:FindFirstChild("HumanoidRootPart")
if not Target_Character or not Target_HRP then return end
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
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 Idle == false then return end
Idle = false
end)
task.spawn(function()
if Target_Detected == true then return end
Target_Detected = true
if Sentry:FindFirstChild(Sentry_Idle.Name) then
Sentry:FindFirstChild(Sentry_Idle.Name):Destroy()
end
Sentry_Module.Play_Sentry_SFX(Sentry_Target_Spotted)
end)
local LookAt_CFrame = CFrame.lookAt(Sentry_Head.Position, Hit_Position)
task.spawn(Sentry_Module.Play_Sentry_SFX, Sentry_Fire)
task.spawn(Fire_Flash)
Sentry_Module.Create_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
if Target_Detected == true then
Target_Detected = false
end
if Idle == false then
Idle = true
end
Sentry_Module.Create_Visualizer(Origin, Hit_Position, Hit_Distance, Color3.fromRGB(255, 0, 0))
end
end
end
task.wait(COOLDOWN)
end
I finished this scripting this sentry with a little bit of help and I wanted to see if there is anything that I could improve! ![]()