I am trying to create a queue system. I have parts named 1, 2, 3, 5 and so on until 100, I would like to move the player to part N, but following all of the preceding parts first in descending order.
For example, let’s say N = 5, I would like the player to move to part 99, 98, 97 and so on until they reach part 5.
So far, this is my solution:
function moveQueue()
for i, v in queue do
task.spawn(function()
for i = totalparts - 1, i, -1 do
v.Character.Humanoid:MoveTo(game.Workspace.DYSQueue.Queues.Path[i].Position)
v.Character.Humanoid.MoveToFinished:Wait(2)
end
end)
end
end
This works completely fine until a second player joins the queue. Once that happens, the first player appears to do the entire queue again.
Also, queue is a table of players, with the index being their current position within the queue.
Here’s a queue module I wrote.
-- Queue module
local Queue = {}
Queue.__index = Queue
function Queue.new()
local self = setmetatable({}, Queue)
self.items = {}
self.front = 0
self.rear = -1
return self
end
function Queue:enqueue(item)
self.rear = self.rear + 1
self.items[self.rear] = item
end
function Queue:dequeue()
if self:isEmpty() then
return nil
end
local frontItem = self.items[self.front]
self.items[self.front] = nil
self.front = self.front + 1
return frontItem
end
function Queue:isEmpty()
return self.front > self.rear
end
function Queue:size()
return self.rear - self.front + 1
end
function Queue:clear()
self.items = {}
self.front = 0
self.rear = -1
end
function Queue:toArray()
local array = {}
for i = self.front, self.rear do
table.insert(array, self.items[i])
end
return array
end
function Queue:toString()
return table.concat(self:toArray(), ", ")
end
return Queue
You can enqueue every part. Then, dequeue them 1 by 1 and move the player to the part.
1 Like