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

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

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?

2 comments

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.

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?

1 answer

✓ Accepted answer

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.

2 comments

0

Worked, thank you!

0

Anytime

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