Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to store players inside of a table?

Asked by 5 years ago
Edited 5 years ago

So I'm trying to create a queue system, where once a player has touched the entrance of the queue zone they will be added or removed accordingly. It adds players fine, but it never removes them. What's going wrong?

add player function


function addPlayer(player) if Queue[player] ~= nil then print("Player already exists") else table.insert(Queue, player) print(player.Name.." added to the queue") end end

remove player function

function removePlayer(player)
    if Queue[player] ~= nil then 
        table.remove(Queue, player)
        print(player.Name.." removed from the queue")
    else 
        print("Player does not exist")
    end
end

the overall Manager

function QueueManager:adjustQueue(player)

    if Queue[player] ~= nil then
        removePlayer(player)
        return false
    else
        addPlayer(player)
        return true
    end

end
0
table.remove takes index as the second argument, not the value Amiaa16 3227 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Use this code and put it in a ModuleScript:

local QueueManager = {}

local queued_plrs = {}

function queue_addPlayer(player)
    if player:IsA("Player") then
        table.insert(queued_plrs, #queued_plrs, player.UserId)
        print("Added player to queue: "..player.Name)
    end
end

function  queue_removePlayer(player)
    if player:IsA("Player") then
        local n = player.UserId
        for i,v in pairs(queued_plrs) do
            if v = n then
                table.remove(queued_plrs, i)
                print("Removed player from queue: "..player.Name)
            end
        end
    end
end

function queue_isInQueue(player)
    if not player:IsA("Player") then
        return false
    end
    for i,v in pairs(queued_plrs) do
        if v == c then
            return true
        end
    end
    return false
end

function QueueManager:ajustQueue(Player)
    if not queue_isInQueue(Player) then
        queue_addPlayer(Player)
    else
        queue_removePlayer(Player)
    end
end

return QueueManager -- If the code is in a ModuleScript. If not remove this line completley
Ad

Answer this question