Hello, I need help with the collection service. I’m having a problem with NPC Ai and Scripts not working when NPC dies using Collection Service.
I’m Extremely new about this since I learned this yesterday. I used this to reduce most of the lag in my game because of a player reporting laggy gameplay.
Here’s the script I used.
local CollectionService = game:GetService("CollectionService")
for _, part in pairs(CollectionService:GetTagged("Employees")) do
game.workspace["PloppyPlaytime-Generators"]["Ploppy Playtime Generator 1"].Generator.Humanoid.Died:Connect(function()
part.Parent.Torso.Alarm:Play()
wait(0.5)
part.Parent.Torso.Alarm:Play()
part.BillboardGui.TextLabel.Visible = true
wait(20)
part.BillboardGui.TextLabel.Visible = false-- basically alerts the employees to your presence
end)
end
I also used it with my Ai Script, here it is
local CollectionService = game:GetService("CollectionService")
for _, part in pairs(CollectionService:GetTagged("TEST")) do
local PathfindingService = game:GetService("PathfindingService")
local Humanoid = part.Humanoid
local Root = part:FindFirstChild("HumanoidRootPart")
local goal = game.Workspace.Goal.Position
local path = PathfindingService:CreatePath()
path:ComputeAsync(Root.Position, goal)
local waypoints = path:GetWaypoints()
wait(3)
for i, waypoint in ipairs(waypoints) do
Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end
end
This Ai Script works but stops working after the humanoid dies.
Here’s a better script I tried using the collection service on.
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local Torso = script.Parent:WaitForChild("Torso")
function walkRandomly()
local xRand = math.random(-75,100)
local zRand = math.random(-75,100)
local goal = game.Workspace.Goal.Position
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for _, waypoint in ipairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
myHuman.Jump = true
end
myHuman:MoveTo(waypoint.Position)
local timeOut = myHuman.MoveToFinished:Wait(1)
if not timeOut then
myHuman.Jump = true
walkRandomly()
end
end
else
wait(1)
walkRandomly()
end
end
Torso.Touched:Connect(function(obj)
if not obj.Parent:FindFirstChild("Humanoid") and math.random(1,24) == 24 then
myHuman.Jump = true
end
end)
function main()
myHuman.WalkSpeed = 16
walkRandomly()
end
while wait(0.1) do
if myHuman.Health < 1 then
break
end
main()
end
I tried making this script into the script that controls all Humanoids, but it failed. Even if it worked, it probably would still malfunction when humanoid dies.
Here is the respawn script I use:
name="Humanoid"
robo=script.Parent:clone()
script.Parent.Humanoid.Died:Connect(function()
wait(2)
robot=robo:clone()
robot.Parent=script.Parent.Parent
robot:makeJoints()
script.Parent:remove()
end)
My GOALS are:
-
create one script that controls all ai and makes them follow a part.
-
Fix collection service malfunctioning due to humanoid dying. (basically keeping the script’s function on respawn)