Nothing in the table is getting removed with table.remove

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i made a role choosing script where it puts players in innocent table then one player is chosen to be sherrif and they will be removed from innocent table and the same happens to murderer

  2. What is the issue? Include screenshots / videos if possible!
    when i ran this script. the innocent table doesnt have anything removed at all. (look in screenshot) (im testing 3 players)
    image
    i had table remove but it doesnt work at all

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes i looked but there’s no topic related to this

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

here’s my script (i added comments)

local players = game:GetService("Players")
local run = game:GetService("RunService")

local function rungame()
	local innocents = {}
	local murderers = {}
	local sherrifs = {}
	
	local plys = players:GetPlayers()
	if #plys >= 3 then
		innocents = plys
		local chosen_murderer = math.random(1, #innocents) --selects a random innocent
		murderers[#murderers+1] = innocents[chosen_murderer] --adds the innocent to that table
		table.remove(innocents, chosen_murderer) --should remove the innocent from the innocent table

		local chosen_sherrif = math.random(1, #innocents) --selects a random innocent
		sherrifs[#sherrifs+1] = innocents[chosen_sherrif] --adds the innocent to that table
		table.remove(innocents, chosen_sherrif) --should remove the innocent from the innocent table
		
		
		local spawnlings = innocents
		for i, v in pairs(sherrifs) do spawnlings[#spawnlings+1] = v end
		for i, v in pairs(murderers) do spawnlings[#spawnlings+1] = v end
		
		for i, v:Player in pairs(spawnlings) do
			v:LoadCharacter()
		end
		print(innocents) --should be one but it stayed the same as before??
		print(murderers) --one
		print(sherrifs) --one
		
		print(#innocents+#murderers+#sherrifs) --It counts to 5 wth?????
	else
		return
	end
	
	local roundtime = 5*60
	local elapsedtime = 0
	repeat
		local dt = run.Heartbeat:Wait()
		elapsedtime += dt
		--print(innocents)
	until (#murderers == 0) or ((#innocents + #sherrifs) == 0) or (elapsedtime >= roundtime)
	
	local results = {murderers,innocents,sherrifs}
	print(results)
	return results
end

any help is appreciated!

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The innocents table doesn’t update itself in the loop because you’re using pairs(), you could fix it by creating a copy of the innocents table and remove the chosen from that.

While this probably won’t help, what I do for table.remove is have the instance or value in the table that I want to remove and use table.find() to do it, like this:

table.remove(myTable, table.find(myTable, whatIWantToRemove))

So I just replaced the spawnlings for loops to spawnlings = plys and now it properly works now!
image
thanks for the suggestions tho

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