Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I tell what the strings table row is? (table.remove help)

Asked by 5 years ago
Edited 5 years ago

So I'm having bit of an issue here. This is my script so far. Also the prints are just for debugging and so you can see what it is.

--source removed

Whoever enters a region (a 2048x2048 part is used to define regions), will be added to a table. That part completely works.

The issue I'm having is when someone leaves a region, which works from my IsTouch script, I don't know what row their name is on, using table.remove.

Table.remove doesn't remove strings, it removes rows. Look below for an example.

--source removed

The example above will remove the second thing in the list, since it's the second in a row. If you were to print the entire table after removing "2", it would print "wont be removed" twice.

My main issue is figuring out what the names row is on, since everyones name is different and they can be added at any time, how can I detect which row the name is on?

0
Use a for loop to iterate through all the elements inside the table until it finds the desired one and store its index on a variable. Le_Teapots 913 — 5y
0
Since I have so many people going into regions, how can I make a variable for one person? Do I have to use DataStore? Chaddaking 60 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I was going to write this as a comment but figured I may as well put this as an answer. As tiraner300 said in the comments, you'll want to loop through the table to find the desired name.

When the player leaves the region, you know that player left? If so, you'll need to use a for loop to find their name in the table by doing the following:

local playerWhoLeft = "Someones_Name"
for iterator, name in pairs(PlayersTouching) do
    if playerWhoLeft == name then
        table.remove(PlayersTouching, iterator)
        break
    end
end

The iterator is the position of the name that matched the playerWhoLeft. The loop starts at the first element when iterator = 1 and increases by 1 for each name being checked. The break ends the loops when you already have found your player because there is no longer a point in traversing the table.

0
Worked, thank you! Chaddaking 60 — 5y
0
Anytime alphawolvess 1784 — 5y
Ad

Answer this question