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

How do I remove a certain player from a table?

So I made a monster that spawns and the monster will not count as a player, otherwise the game will never end. But I cannot figure out how to do it. Can someone help? Here's my error: 20:14:09.658 - ServerScriptService.Script:61: bad argument #1 to 'remove' (table expected, got Object)

And here's the script where the error came from.



 local status = game.ReplicatedStorage.status
local timer = game.ReplicatedStorage.timer
local plrsLeft = game.ReplicatedStorage.plrs
local gameTime = 70
local intermission = 20
local maps = game.ServerStorage.Maps
local id = game.ReplicatedStorage.ImageId

while true do
	status.Value = "Waiting for more players."
	plrsLeft.Value = "0/".. game.Players.NumPlayers
	repeat wait(1) until game.Players.NumPlayers >= 1
	for i =  intermission,0,-1 do
		status.Value = "Intermission"
		id.Value = "http://www.roblox.com/asset/?id=4546507417"
		local minutes = math.floor(i/60)
	    local seconds = math.floor(i%60)
	    timer.Value = (string.format("%i:%.2i", minutes, seconds))
	    wait(1)
		local plrs = {}
		for e, player in pairs(game.Players:GetPlayers()) do
			if player then
				table.insert(plrs,player)
				if i == 0 then
					status.Value = "Generating map."
					wait(2)
					local availableMaps = maps:GetChildren()
					local chosenMap = availableMaps[math.random(1,#availableMaps)]
					local clonedMap = chosenMap:Clone()
					clonedMap.Parent = game.Workspace
					wait(1)
					status.Value = clonedMap.Name.." loaded, get ready."
					wait(2)
					local spawns = clonedMap:FindFirstChild("SpawnPoints")
					local availableSpawns = spawns:GetChildren()
					local character = player.Character
					plrsLeft.Value = #plrs.."/".. game.Players.NumPlayers
					character.HumanoidRootPart.CFrame = availableSpawns[1].CFrame
					table.remove(availableSpawns,1)
					for x = gameTime,0,-1 do
						id.Value = "http://www.roblox.com/asset/?id=4546507994"
						local minutes = math.floor(x/60)
					    local seconds = math.floor(x%60)
					    timer.Value = (string.format("%i:%.2i", minutes, seconds))
					    wait(1)
						status.Value = "Game in progress."
						character.Humanoid.Died:Connect(function()
							table.remove(plrs,player)
							print(player.Name.." has died/left the game.")
						end)
						if x <= 20 then
							id.Value = "http://www.roblox.com/asset/?id=4546508680"
						end
						
						--// Pick a player to be monster ;)
						local num = math.random(1,#plrs)
						plrs[num].Character = game.ServerStorage.Telamon
						game.ServerStorage.Telamon.Parent = workspace
						wait(0.000000000000025)
						workspace:FindFirstChild("Telamon").HumanoidRootPart.CFrame = clonedMap.MonsterSpawn.CFrame
						table.remove(plrs[num],player)
						
						if plrs[num] == 1 and #plrs == 0 then
							status.Value = "Telamon wins."
							plrs[num].leaderstats.Coins.Value = plrs[num].leaderstats.Coins.Value + 100
							wait(1)
							break
						elseif x == 0 then
							status.Value = "Time out."
							wait(1)
							break
						elseif plrs[num] == nil then
							status.Value = "Telamon left."
							wait(1)
							break
						end
						player:LoadCharacter()
						clonedMap:Destroy()
						status.Value = "Gamer over."
						plrsLeft.Value = "0/".. game.Players.NumPlayers
						timer.Value = 0
						wait(2)
						
						
				end
				player:LoadCharacter()
			break
			end
			end
		end
	end
end





1 comment

0

does the table conatain Player Objects, or characters?

1 answer

✓ Accepted answer

table.remove removes the element of an array via numerical index of said element through the passed table. You gave it the affiliated Player Object at index num Instead of telling it what table it should be removing the element from.

To fix this, pass the array as expected by the first argument, and numerical location required by the second argument; we are fortunate since the location is already declared in num

table.remove(plrs, num)

Please remember to accept if this answer helps you out!

4 comments

0

ahh

0

actually, that might work, great answer!

0

but what would I do for the last elseif statement?

0

Whatever element is located at plrs[num] is no longer available. You’ll have to reorganize the code you have written if it causes issues, or justify why you had to remove the element altogether, then compensate.

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