Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to store players inside of a table?

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

1 comment

0

table.remove takes index as the second argument, not the value

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.