Object wont remove from table

  1. What do you want to achieve?
    I want the table to remove an object if an object transparency is 1 and then destroys so that once all index has been removed excpet for the one that the object name is set to the players name. That way the script would call an object name that hasn’t been removed from the table instead all the values in the folder

  2. What is the issue?
    I probably used table.remove incorrectly and thus it doesnt work

  3. What solutions have you tried so far?
    I tried looking for solutions on the devforum but it didn’t help,

-- If the rest of the object's transparency is 1 then those object should be removed from the table
for i, tile in pairs(tiless) do 
		if tiless[i].Transparency == 1 then
			tiless[i]:Destroy()
			table.remove(tiless, i)
		end

and that code doesn’t work, so how can I properly remove an object from a table after it’s being destroyed?

tile:Destroy() should help, instead of using numeric indexing to get a part, you’re using a combiniation of wich the adress from the Pair is unused.
I’d highly reccomend on keeping an eye on your use of for loops and arrays.
For instance,
plyrs = {} and then a for loop to insert the players into the table can be simplified to
plyrs = game.Players:GetChildren()
That part is also wrong btw, you’re using the object from the pairs instead of the index (here _) as an index to be put into the array plyrs.

Hello, SpinTower! Lets take a look at some of your code, and review it.

	for i = 2, 0, -1 do
		Status.Value = "Intermission"
		wait(0.20)
		Status.Value = "Intermission."
		wait(0.20)
		Status.Value = "Intermission.."
		wait(0.20)
		Status.Value = "Intermission..."
		wait(0.20)
	end

Here you’re using a for-loop to change the value of a, well the entire reason of using a for loop is so you don’t have to perform the method that you did! Rather than changing the Status.Value every .20 seconds, how about we reduce your code so that it does it by itself?

local intermissionText = 'Intermission...' -- This is defining the variable, intermissionText, as "Intermission"
--#intermissionText is the length of intermissionText, it returns a number value.
for i = 1, #intermissionText do
    Status.Value = string.sub(intermissionText, 1, i)
    wait(.20) --Waits 0.20 seconds before running again!
end

In effect, this reduces your code down to only 4 simple lines of code. You should also do the same thing we did here for the Teleporting players loop and the Setting up game loop!

Lets move onto the availablespawnpoints.

Your loop currently loops through and says if the player has a character, then move them to the first spawn point available- then remove that spawn point from the availablespawnpoints’s array. You are not physically destroying the spawn-part from workspace. You could solve this by doing:

local spawnPoint = availablespawnpoints[1]
character.HumanoidRootPart.CFrame = aspawnPoint.CFrame
spawnPoint:Destroy() --This will physically remove the tile from the workspace.
for i, tile in pairs(Tiles:GetChildren()) do
	tile.Touched:Connect(function(character)
		tile.Name = player.Name
	end)
	table.insert(tiless, tile)
end

In this code, you’re creating a new connection every time the tile is touched… but you have no debounce! So this code will run very single time a player is has touched the title. If I were you, I’d insert a BoolValue into the tile, named IsOccupied and if IsOccupied == true then return end.
Though, you should be sure to destroy all of these connections that you’ve made at the end of the round. Else, sever memory will become super clogged and begin to lag.

Now, moving onto the final code you put:

for i, tile in pairs(tiless) do 
    if tiless[i].Transparency == 1 then
	    tiless[i]:Destroy()
	    table.remove(tiless, i)
end

There’s simply no need to index tiless[i] when you could simply do tile.Transparency. Ultimately brining your code down to:

for i, tile in pairs(tiless) do 
    if tile .Transparency == 1 then
	    tile:Destroy()
	    table.remove(tiless, i)
end

Keep in mind I’m not saying this is the ultimate solution to your code, I am saying these are some things you could to do make your code look cleaner, and run more efficient. You should be sure that you are not just removing the TILE from the array of tiles. You have to physically destroy the tile item, else it will still appear in workspace! When you do tile[i] all you’re doing is removing the tile from the array of tiles. The tile object/instance will still exist.

1 Like

That’s not what I meant to do. I meant you create the BoolValue in workspace and put it in the part, not via the script.
Your script should then look like this:

for i, tile in pairs(Tiles:GetChildren()) do
	tile.Touched:Connect(function(character)
		if tile:FindFirstChild('isOccupied').Value == false then
            tile.Name = player.Name
            tile:FindFirstChild('isOccupied').Value = true
        end
	end)
    table.insert(tiless, tile)
end

Could you add a print before the tile:Destroy(), tell me if it prints.

image

This code is a mess! How did you get to do this?

But don’t worry, I’m here to help you:

  1. There’s no need to create an players table. game:GetPlayers() already does the job. You basically cloned it.

  2. You don’t need to use so many lines to add an ‘.’ to an string. You can simply use a numeric for loop, and set the max number to the amount of dots you want. Like so:

local Text = 'Intermission'
for DotIndex = 1, 3 do
    Text = Text.. '.'
    Status.Value = Text
    wait(Time)
end
-- You'll do the same with 'Teleportings players' and 'Setting up game'.
  1. You don’t need to check if the player is nil / false, but it can’t be.
  2. I suggest you set a variable which represents a player’s character, to PlayerObject.Character or PlayerObject.PlayerAdded:Wait().
  3. Once again, you don’t need to make an new table for descendants / childs of a object. The :GetChildren() and :GetDescendants() function already does that.
  4. On the if statement where you check if character is nil or false, you’ve put a else in it, and tried to change the .CFrame property of the HumanoidRootPart. That wouldn’t work, since, if the character is nil, how can you index something of it?
  5. You’ve put an unnecessary else on the if statement where you check if the player is nil or false. Though this is not necessary because you should straight off remove that if statement.
  6. You should replace what you’re going to loop through, in the loop you loop through the plyrs table (Which should be removed, as stated earlier.) to game.Players:GetPlayers(), but I shouldn’t even need to say this.
  7. You don’t need to check if the player is nil or false.
  8. You addressed the character, but then you used player.Character. You basically made the variable useless. And it really shouldn’t be made because you’ve only used it once.
  9. Loop through Tiles:GetChildren() instead.
  10. Once again, you’ve tried to index something of the character, when it was nil. That will error!
  11. Unnecessary else on the if statement where you check if player is nil or false (Which should be removed, as stated earlier.)
  12. Another loop where you loop through tiless, which should be Tiles:GetChildren() instead.
  13. i resembled 3 values where you made a numerical loop with start being tick().
  14. I got bored after this, good luck fixing the other things though.
1 Like

Thanks for the feedback but that isn’t what i’m looking for. I’m having problem with this line here:

for i, tile in pairs(Tiles:GetChildren()) do 
    if tile .Transparency == 1 then
	    tile:Destroy()
	    table.remove(tiless, i)
end

The problem is it doesn’t remove the number of indexes, it now doubles
image
I had about 54 indexes now it showing 104

Can you send the current code? I don’t want a file. I want the code.

Also, how did it even double?

let me know if you got a solution for me

Depending on how you set up everything, you could try using Tiles:GetDescendants instead, just in case searching through only the direct children is the problem.

It would be revised to this:

for i, tile in pairs(Tiles:GetDescendants()) do
    if tile.Transparency == 1 then
        tile:Destroy()
        table.remove(tiless, i) -- What is this?? A typo?

Also, what is “tiless” referring to? Is it a typo?

Oh just another table I inserted from Tiles:GetChildren()