So I’m doing a script that handles with the Enemy on my game, it’s a RPG one. I’ve made a script that handles with each enemy on the game, it contains parts that handles Properties, Follow, Damage, Award and stuff. Althought, I am witnessing a problem with the Follow function, Respawn, Award function. I don’t know what is happening and I need help to improve my script so everything is fine.
**The Script: **
local players = game:GetService("Players")
local mobs = workspace:WaitForChild("Mobs")
local function Damage(hit, _M)
if (not players:GetPlayerFromCharacter(hit.Parent)) then return end
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (not plr.Character) then return end
local chr = plr.Character
if (not chr:FindFirstChild("Humanoid")) then return end
local hum = chr.Humanoid
if chr:FindFirstChild("ForceField") then return end
hum.Health = hum.Health - _M.MobDamage
end
function FindTarget(_M, Enemy)
local maxDistance = _M.FollowDistance
local mob = Enemy.Parent
for _, v in pairs(game.Players:GetPlayers()) do
if v.Character and v.Character:FindFirstChild("Humanoid") then
local torso = v.Character:FindFirstChild("Torso")
if torso and (torso.Position - mob.Torso.Position).magnitude <= maxDistance then
local target = torso
task.wait(0.1)
return target
end
end
end
end
local function Respawn(_M, Mob)
local MobClone = Mob:Clone()
task.wait(_M.RespawnTime)
Mob:Destroy()
MobClone.Parent = mobs
MobClone:MakeJoints()
end
local function Award(Mob, Enemy, _M)
if (Mob == nil) or (Enemy == nil) then return end
if (not Enemy:FindFirstChild("Player_Tag")) then return end
local Tag = Enemy:FindFirstChild("Player_Tag")
if (not Tag) and (not Tag.Value) then return end
local plr = Tag.Value :: Player
if (not plr:FindFirstChild("leaderstats")) then return end
local stats = plr:FindFirstChild("leaderstats")
if (not plr:FindFirstChild("EXP")) then return end
if (not plr:FindFirstChild("Gold")) then return end
task.wait(0.1)
if stats.LOVE.Value < _M.LevelRequiredToKill then return end
plr.EXP.Value += _M.AwardedXP
plr.Gold.Value += _M.AwardedGold
end
local function TeleportOnKill(_M, Enemy)
if (not _M.DoesMobTeleportAfterKill) then return end
local tag = Enemy:FindFirstChild("Player_Tag")
if (tag==nil) or (tag.Value==nil) then return end
print(tag)
local chr = tag.Value.Character or tag.Value.CharacterAdded:Wait()
local root = chr:FindFirstChild("HumanoidRootPart")
if (chr==nil) and (root==nil) then return end
root.CFrame = _M.TeleportLocation + Vector3.new(0,3,0)
end
local Attributes = function(Enemy, module)
local UI = Enemy.BillboardGui
local textlabel = UI:WaitForChild("TextLabel")
local MName = module.MobName :: string
local MRank = module.RankName :: string
Enemy.MaxHealth = module.MobHealth
Enemy.Health = Enemy.MaxHealth
Enemy.WalkSpeed = module.MobWalkSpeed
textlabel.Text = MName.."["..MRank.."]"
end
local DealDamage = function(Enemy,module, mob)
local debounce = false
mob.Torso.Touched:Connect(function(hit)
if (Enemy.Health < 1) then return end
if debounce then return end
debounce = true
Damage(hit, module)
task.wait(.7)
debounce = false
end)
end
local OnDied = function(Enemy, module, mob)
Enemy.Died:Connect(function()
coroutine.wrap(Respawn)(module, mob)
coroutine.wrap(Award)(mob, Enemy, module)
TeleportOnKill(module,Enemy)
end)
end
local Follow = function(Enemy, _M)
while _M.FollowDelay do
local target = FindTarget(_M, Enemy)
print(target)
if target then
Enemy.Humanoid:MoveTo(target.Position)
end
end
end
local function LoadCharacter(Enemy, _M, instance)
Attributes(Enemy, _M)
Follow(Enemy,_M)
end
for _,v in pairs(game:GetService("CollectionService"):GetTagged("Mobs")) do
if (not v:IsA("Model")) then return end
if (not require(v.MobConfig)) then return end
if not v:FindFirstChild("Enemy") then return end
local Enemy = v.Enemy :: Humanoid
local _M = require(v.MobConfig)
LoadCharacter(Enemy,_M,v)
OnDied(Enemy, _M, v)
coroutine.wrap(DealDamage)(Enemy, _M, v)
end
mobs.ChildAdded:Connect(function(c)
if not c:IsA("Model") then return end
if not c:FindFirstChild("Enemy") then return end
local Enemy = c:FindFirstChild("Enemy")
local _M = require(c.MobConfig)
LoadCharacter(Enemy, _M, c)
OnDied(Enemy, _M, c)
coroutine.wrap(DealDamage)(Enemy, _M, c)
end)
Thank you in advance.