Table.insert() doesn't work

G’day. Basically, using the table.insert() doesn’t work even when doing something like: Clients[plr] = true, it doesn’t work. It prints out: {}. Also, when adding a print to the playerAdded it works therefore there is no problem with these at all except the table.

function MoneyService:ServerStart()
	local Clients = {}

	local function addPlayer(plr)
		if not table.find(Clients, plr) then
			table.insert(Clients, plr)
		end
	end

	local function removePlayer(plr)
		local index = table.find(Clients, plr)
		if index then
			table.remove(Clients, index)
		end
	end

	for _, player in ipairs(Players:GetPlayers()) do
		addPlayer(player)
	end

	Players.PlayerAdded:Connect(addPlayer)
	Players.PlayerRemoving:Connect(removePlayer)
	
	print(Clients)

	for _, client in ipairs(Clients) do
		if client:IsA("Player") then
			MoneyService.ServerStartFunc.Client[client.Name] = MoneyService.ServerStartFunc.Client[client.Name] or {}
			MoneyService.ServerStartFunc.Client[client.Name]["Print"] = MoneyService.Client.Print
			MoneyService.ServerStartFunc.Client[client.Name]["SayNothing"] = MoneyService.Client.SayNothing
		end
	end

	print("Server started!")
end

Please I really need help, thanks.

Maybe try replacing this:

	local function addPlayer(plr)
		if not table.find(Clients, plr) then
			table.insert(Clients, plr)
		end
	end

With this:

	local function addPlayer(plr)
		if not table.find(Clients, plr) then
			table.insert(Clients, #Clients+1, plr)
		end
	end

or this: (edit: I think I have had this problem before and this fixed it for some reason)

	local function addPlayer(plr)
		if not table.find(Clients, plr) then
			Clients[#Clients+1] = plr
		end
	end

Um… Nope, it didn’t work. I have no idea what’s the problem. Also, it’s a ModuleScript.

Wait… I’ve fixed the issue, I only had to add task.wait(some seconds) and printing the table, and it worked just fine!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.