I am trying to achieve a “NPC lineup system”, I have successfully created the part where a line is picked at random and NPCs are moved into that line. But I am also trying to make the first rig in the table be destroyed in 5 seconds, starting from when the rig was inserted. I have used tick() to do this, however, I ran into an error as seen in this Gyazo.
https://gyazo.com/c0e27caeafa4d6b0a24a4a8129051e79 (Rig moves to it’s correct position, waits 5 seconds then is moved to a part then destroyed, however next rig in line is also doing the same thing without waiting 5 seconds.)
game:GetService("RunService").Heartbeat:Connect(function()
if rigTableC[1] then
local thisRig = rigTableC[1]
print(tick() - last)
if tick() - last >= 5 then
print("greater than 5")
if thisRig then
thisRig.Humanoid:MoveTo(workspace.LeavingShop.Position)
wait(1)
thisRig:Destroy()
table.remove(rigTableC, 1)
print(rigTableC[1])
print(unpack(rigTableC))
for i, rig in pairs(rigTableC) do
local targetPos = workspace.DestinationPart4.CFrame * Vector3.new(0, 0, 4 * i)
rig.Humanoid:MoveTo(targetPos)
end
elseif thisRig.Parent ~= workspace then
table.remove(rigTableC, 1)
else
return end
end
end
end)
--- Part of script checking if the random point is certain point then do everything below.
if pickedPoint == workspace.DestinationPart3 then
local targetPos = workspace.DestinationPart4.CFrame * Vector3.new(0,0,4 * #rigTableC)
humanoid:MoveTo(targetPos)
humanoid.MoveToFinished:Wait()
table.insert(rigTableC, rig)
if rigModel == rigTableC[1] then
last = tick()
local randomPoint = dictionary[math.random(1, #dictionary)]
local random = unpack(randomPoint)
rigModel.Head.BillboardGui.Enabled = true
rigModel.Head.BillboardGui.ImageLabel.TextLabel.Text = random
end
end