-
What does the code do and what are you not satisfied with?
The code is supposed to make a Dummy follow and kill players in range. -
What potential improvements have you considered?
I tried to organise the code by adding sections of used items. -
How (specifically) do you want to improve the code?
I want to make it more efficient, I really don’t know how to organise code properly and I need some tips on how to make it work and look better.
Okay, so to make the main thing work is by putting a while wait() loop and letting all of the code run inside of it.
Then I added some variables and functions inside that might be changed in the mid-game, if I want to configure something while developing.
Code:
-- Name: Finding Script
-- Written by: MARKedGamingR / MarkedThing
-- Usage: Pathfinding to nearest player.
-- Originally Created: 26.3.2021
-- Recreated: 29.3.2021
-- Updated: 29.3.2021 15:44, Jerusalem Time
--// Services //--
local SERVICES = {
Tween = game:GetService("TweenService")
}
--// Variables //--
--// Core
local Model = script.Parent
local Primary = Model.PrimaryPart
local ModelHuman = Model.Humanoid
--// Other
local Table_Folders = {
Points = Model.Points,
Config = Model.Config,
Debug = Model.Debug
}
local IdleSound = Primary.Idle
local ChaseSound = Primary.Chase
--// Toggles
local Target_View = nil
local Target_Chase = nil
local Target_Point = nil
local Tweening = false
--// Animations
local Table_Anim = {
Idle = ModelHuman:LoadAnimation(script.Idle),
Walk = ModelHuman:LoadAnimation(script.Walk)
}
--// Tweening
local function movePrimary(Target,Studs)
Primary.CFrame = CFrame.new(Primary.CFrame.Position,Target.CFrame.Position) + Primary.CFrame.LookVector * (Studs * 12)
end
--// Debug
local Display_Kill = Table_Folders.Debug.KillRad
local Display_Chase = Table_Folders.Debug.ChaseRad
--// Functions //--
Table_Anim.Idle:Play()
local function RemoveTarget(Part)
if Target_View == Part then
Target_View = nil
Target_Point = nil
end
if Target_Chase == Part then
Target_Chase = nil
end
end
--// Main
while wait() do
local Studs = Table_Folders.Config:GetAttribute("Studs")
local ChaseRadius = Table_Folders.Config:GetAttribute("ChaseRadius")
local KillRadius = Table_Folders.Config:GetAttribute("KillRadius")
local DebugMode = Table_Folders.Config:GetAttribute("DebugMode")
Display_Chase.Size = Vector3.new(ChaseRadius*2,ChaseRadius*2,ChaseRadius*2)
Display_Kill.Size = Vector3.new(KillRadius*2,KillRadius*2,KillRadius*2)
if DebugMode == true then
Display_Chase.Transparency = 0
Display_Kill.Transparency = 0
Primary.Transparency = 0
Display_Chase.CFrame = Primary.CFrame
Display_Kill.CFrame = Primary.CFrame
for i,Point in pairs(Table_Folders.Points:GetChildren()) do
Point.Transparency = 0
end
else
Display_Chase.Transparency = 1
Display_Kill.Transparency = 1
Primary.Transparency = 1
for i,Point in pairs(Table_Folders.Points:GetChildren()) do
Point.Transparency = 1
end
end
for i,Player in pairs(game.Players:GetChildren()) do
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local RootPart = Character:FindFirstChild("HumanoidRootPart")
local Light = Character:FindFirstChild("Light")
if Character and Humanoid and RootPart and Light then
local RootMag = (RootPart.Position - Primary.Position).Magnitude
--// Target View
if Humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
if Target_View == nil and Target_Chase == nil then
Target_View = RootPart
elseif Target_View and Target_Chase == nil then
if RootMag < (Target_View.Position - Primary.Position).Magnitude then
Target_View = RootPart
end
--// Target Point
for i,Point in pairs(Table_Folders.Points:GetChildren()) do
local PointMag = (Point.Position - Target_View.Position).Magnitude
if Target_Point == nil then
Target_Point = Point
else
if PointMag < (Target_Point.Position - Target_View.Position).Magnitude then
Target_Point = Point
end
end
end
end
if RootMag <= ChaseRadius and Light.Value == true then
if Target_Chase == nil then
Target_Chase = RootPart
elseif Target_Chase then
if RootMag < (Target_Chase.Position - Primary.Position).Magnitude then
Target_Chase = RootPart
end
end
elseif RootMag > ChaseRadius and Target_Chase == RootPart then
Target_Chase = nil
ChaseSound.Playing = false
if Table_Anim.Walk.IsPlaying == true then
Table_Anim.Walk:Stop()
end
end
elseif Humanoid:GetState() == Enum.HumanoidStateType.Dead then
RemoveTarget(RootPart)
end
--// Handler
if Target_Point and Target_Chase == nil and (Primary.Position - Target_Point.Position).Magnitude > 2 then
movePrimary(Target_Point,Studs/2)
elseif Target_Chase then
movePrimary(Target_Chase,Studs)
if Table_Anim.Walk.IsPlaying == false then
Table_Anim.Walk:Play()
end
if ChaseSound.Playing == false then
ChaseSound.Playing = true
end
if Target_Chase == RootPart and Humanoid.Health > 0 and (Target_Chase.Position - Primary.Position).Magnitude <= KillRadius then
Humanoid.Health = 0
ChaseSound.Playing = false
if Table_Anim.Walk.IsPlaying == true then
Table_Anim.Walk:Stop()
end
local KillSound = Primary.KillSound:Clone()
KillSound.Parent = Target_Chase
KillSound:Play()
local KillAttach = Primary.KillAttach:Clone()
KillAttach.Parent = Target_Chase
KillAttach.Emitter:Emit()
Target_Chase = nil
end
end
end
end
Primary.Light.Brightness = Random.new():NextNumber(0,2)
Primary.Transparency = 1
end
Again, I need some feedback on how to make it look better and if there are any problems, please point it out.
P.S: I’m new on the dev forum, Heya!