Issue with tables

Hello, I’ve been working on a waiting out button for a round based game, however I’m having issues with the tables, this is my current script for reading when the person pressed the button:

script.Parent.MouseButton1Click:Connect(function(PLRpressed)
if script.Parent.Text == “Waiting for a game” then

	script.Parent.Text = "Waiting out"
	game.ReplicatedStorage.WaitingOut:FireServer(PLRpressed)
	
else
	
	script.Parent.Text = "Waiting for a game"
	game.ReplicatedStorage.WaitingForAGame:FireServer(PLRpressed)
	
end

end)

This is where the server picks up on the button press:

  1. game.ReplicatedStorage.WaitingOut.OnServerEvent:Connect(function(PLRpressed)
    1. local WaitingOutButtonPress = PLRpressed
    2. print(WaitingOutButtonPress)
    3. table.remove(PLRwaitingList, WaitingOutButtonPress)
    4. print(“WORKED”)

end)

I’m receiving a error expecting this:

ServerScriptService.Timer:68: invalid argument #2 to ‘remove’ (number expected, got Instance) - Server - Timer:68

Though I’m using a similar script to add them to the table: table.insert. I’m not sure why it’s not expecting remove but any help is appreciated!

Not sure how can I help, but I should mention that the table.remove function removes based on index.

game.ReplicatedStorage.WaitingOut.OnServerEvent:Connect(function(Player,PLRpressed) try replacing the respective line with this, your first parameter is actually the player not what you send

1 Like

Yes, but the first line on the first script sees who pressed the button right, PLRpressed?

oh okay i see, you should remove the stuff based on indexes. try doing this,
local numb=0
for i,v in next, PLRwaitingList do
numb=numb+1
if v==WaitingOutButtonPress then
break
end
end
table.remove(PLRwaitingList,numb)

i haven’t tested the code, but maybe this gives you the idea, right? you need to include where the object is in the array and then put that number inside of the table.remove()

I see how it could work but I don’t see how marking the player(PLRpressed) as a number could work.

it’s because the table.remove() only accepts the table AND the position of your object. it does not accept objects themselves sadly. you need to find where it’s at the table and then include that there. so like table.remove(Table,PositionNumber) should be what you should use and it’ll work.

the idea of the code i sent was to go through the table and find the position number then put it into the table.remove()

Where would the player be marked as a number?

okay im assuming that WaitingOutButtonPress if the item you’re trying to remove, right? if so, then you would go through your table and find where it is and then store the position number and then after that call table.remove(YourTable,PositionNumber), go look through the code i provided for the idea

I see, but this is the rest of my code I just want to make sure it would still run.

–Game Stats
local roundLength = 60 --Change this value to edit the ingame length
local preroundLength = 15 --Change this value to edit the preround length
local InRound = game.ReplicatedStorage:WaitForChild(“InRound”)
local Lobbyspawn = game.Workspace.LobbySpawn
local GameSpawn = game.Workspace.GameSpawn
local Status = game.ReplicatedStorage:WaitForChild(“Status”)
local PLRwaitingList = {}

InRound.Changed:Connect(function()
if InRound.Value == true then

	local UpperTilesClone = game.ReplicatedStorage.UpperTiles:Clone()
	UpperTilesClone.Parent = game.Workspace
	UpperTilesClone.Name = "UpperTiles"

	local BottomTilesClone = game.ReplicatedStorage.BottomTiles:Clone()
	BottomTilesClone.Parent = game.Workspace
	BottomTilesClone.Name = "BottomTiles"
	
	game.Workspace.Bell.Playing = true
	
	wait(1)
	
	for _, player in pairs(game.Players:GetChildren()) do
		local char = player.Character
		char.HumanoidRootPart.CFrame = GameSpawn.CFrame
		
		game.Workspace.ThemeMusic:Play()
		
	end
else
	for _, player in pairs(game.Players:GetChildren()) do
		local char = player.Character
		char.HumanoidRootPart.CFrame = Lobbyspawn.CFrame
		
		wait(1)
		
		game.Workspace.UpperTiles:Destroy()
		game.Workspace.BottomTiles:Destroy()
	end
	end

end)

local function roundTimer()
while wait() do
for i = preroundLength, 1, -1 do
local Click = game.Workspace.TimerClick
Click:Play()
InRound.Value = false
wait(1)
Status.Value = “Preround: “… i …” seconds left!”
end
for i = roundLength, 1, -1 do
InRound.Value = true
wait(1)
Status.Value = “Game: “… i …” seconds left!”
end
end
end

spawn(roundTimer)

game.ReplicatedStorage.WaitingOut.OnServerEvent:Connect(function(PLRpressed)
local WaitingOutButtonPress = PLRpressed
print(WaitingOutButtonPress)
table.remove(PLRwaitingList, WaitingOutButtonPress)
print(“WORKED”)

end)

game.ReplicatedStorage.WaitingForAGame.OnServerEvent:Connect(function(PLRpressed)
local AwaitingGameButtonPress = PLRpressed
print(AwaitingGameButtonPress)
table.insert(PLRwaitingList, AwaitingGameButtonPress)
print(“Worked”)
print(PLRwaitingList)

end)

well you haven’t done the update though so… try doing what i said then running it

1 Like
local Index=0
for i,v in next,  PLRwaitingList do
if v==PLRpressed then
break
end
Index=Index+1
end
table.remove(PLRwaitingList,Index)
local PlayerIndex = table.find(PLRwaitingList, PLRpressed) -- get the index of the player in the table
if PlayerIndex then
    table.remove(PLRwaitingList, PlayerIndex) -- remove element at the index
end

It worked! Thank you so much Katzue!

1 Like

also uhh try making my comment as the solution so others can see it if they have similar issues, I think I explained table.remove() in detail

uhhhh I think it’s marked, it appears marked as solution to me…