I have a year and a half of experience but I think i’m pretty good for my experience
You could do something like advanced NPC’s that walk, chat, and do other activities. Like if another NPC or player dies, the NPC’s would like swarm around the body or the NPC’s would run if they heard gunshots
alrighty this is the class i made for it, rate the code
local module = {}
module.__index = module
local chats = {
"Good weather, eh?",
"How ya doing?",
"My favorite person in the world!",
"One Two Three?",
"Konichiwazap"
}
local swarmingChats = {
"He just died!",
"How'd he die?",
"What the hell!",
"I'm so scared!"
}
local cs = game:GetService("CollectionService")
function module.new(rig, chatDistance)
local self = setmetatable({},module)
self.Rig = rig
self.Humanoid = rig.Humanoid
self.HumanoidRootPart = rig.HumanoidRootPart
self.chatDistance = chatDistance
self.walkDB = false
self.walkCooldown = 3
self.chatDB = false
self.chatCooldown = 5
self.Swarming = false
return self
end
function module:checkForPlayers()
local closestChar = nil
for i,v in game.Players:GetPlayers() do
local char = v.Character
local hrp = char.HumanoidRootPart()
local dist = (hrp.Position - self.HumanoidRootPart.Position).Magnitude
if not closestChar then
closestChar = char
elseif dist < (closestChar.HumanoidRootPart.Position - self.HumanoidRootPart.Position) then
closestChar = char
end
end
if (closestChar.HumanoidRootPart.Position - self.HumanoidRootPart.Position) < self.chatDistance then
return closestChar
end
end
function module:selectRandomPoint()
local Points = workspace.Points:GetChildren()
return Points[math.random(1, #Points)]
end
function module:Swarm(dead)
self.Swarming = true
self.Humanoid:MoveTo(dead.HumanoidRootPart.Position)
task.wait(12)
self.Swarming = false
end
function module:init()
self.Connection = game:GetService("RunService").Stepped:Connect(function()
if not self.Swarming then
if not self.walkDB then
self.walkDB = true
self.Humanoid:MoveTo(self:selectRandomPoint().Position)
task.wait(self.walkCooldown)
self.walkDB = false
end
end
if not self.chatDB then
self.chatDB = true
local chats = if self.Swarming then swarmingChats else chats
game:GetService("Chat"):Chat(self.Rig.Head, chats[math.random(1, #chats)])
task.wait(self.chatCooldown)
self.chatDB = false
end
end)
for i,v in cs:GetTagged("NPC") do
if v == self.Rig then continue end
v.Humanoid.HealthChanged:Connect(function(health)
if health == 0 then
self:Swarm(v)
end
end)
end
cs:GetInstanceAddedSignal("NPC"):Connect(function(instance)
instance.Humanoid.HealthChanged:Connect(function(health)
if health == 0 then
self:Swarm(instance)
end
end)
end)
end
function module:destroy()
self.Connection:Disconnect()
self = nil
end
return module
1 Like
Looks good and seems like it does the job.
I would prefer using .functions for functions like check for players which does not require self for organization.
Like module.checkForPlayers since it does not require self keyword.
I would suggest more projects for learning such as projectile aim prediction, procedural animations, or a spawn npc menu if you are interested.
Try creating an Enemy with 4 legs, with A* integrated, and Inverse kinematics for each and every one of its limbs