generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: table.insert and table.remove

Previous Thread :: Next Thread 
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
21 Dec 2011 12:46 PM
Blah, really didn't study this that much. Would this work? Not for a game or anything, just making sure I know this stuff.

local pAdded = {}
game.Players.PlayerAdded:connect(function(p)
table.insert(pAdded,1,p)
p.PlayerRemoving:connect(function(r)
table.remove(pAdded,1,p)
end)
end)

Prertty sure the insert works but not the remove :P
Report Abuse
L2000 is not online. L2000
Joined: 03 Apr 2008
Total Posts: 77448
21 Dec 2011 12:57 PM
table.remove() doesn't work with the name. You have to have it like this:

for i,v in pairs(pAdded) do
if v == p then
table.remove(pAdded, i)
end
end

As for table.insert(), it only takes two values: The table, pAdded, and what you're adding, which is p (As far as I know - I've tried with a number before, and it never worked for me). So, what you would do for that is:

table.insert(pAdded, p)
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
21 Dec 2011 01:00 PM
Two questions

For table.remove() Would you have to say the index number if there is > than 2 values(v)

For table.insert when you say (pAdded,p) and another person comes in what would happen? Would it be replaced or just moved into another index?
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 01:25 PM
plrs = {}

function findp(n)
for _,v in pairs(plrs) do
if v == n then
return _
end
end
return false
end

game.Players.PlayerAdded:connect(function(np)
table.insert(plrs,np)
end)
game.Players.PlayerRemoving:connect(np)
if tonumber(findp(np)) then
table.remove(plrs,findp(np))
end
end)
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 01:26 PM
table.insert(table,obj)
table.remove(table,index)

Using table.insert, you have the first argument as the table name. The second argument is whatever you are placing into the table. You don't necessarily need a third argument.

Using table.remove, you have the first argument as the table name, as well. The second argument, however, HAS to be the index. For example, a number.
This is why I created a separate function to find the number in the table that the object is.
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
21 Dec 2011 01:33 PM
@grimm

If I did say

table.insert(table,p,1)--WOuld it still work?
And If two people came on, what would happen? Would the first value be replaced?

And

table.remove(table,1)--Removes [1]
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 01:37 PM
If you want three arguments to set the exact location..

table.insert(tablename,1,object)

Please note that 'table' can not be used as a table's name. The reason for this is table manipulation. You are not allowed to use any Lua words as variables.

table.remove(tablename,1)

That would remove the first item in the table.

Now, it would not work like that, when you tried using it in the type of script I made. The reason for this is that you have to remove the player that left, not just the first one in the table, as any player can leave.
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 01:39 PM
Using..

table.insert(tablename,1,obj)

That would insert whatever 'obj' to 'tablename', in the first position.

If you currently had..

tablename = {"One","Two"}

and used

table.insert(tablename,1,"Three"}

tablename would then be..

tablename = {"Three","One","Two"}
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
21 Dec 2011 01:44 PM
What if I said

table.insert(table,p.Name)--If a second player came, would the second players index be [1] or [2]?


for i,v in pairs(table:GetChildren()) do
string.match(p.Name,v)
table.remove(table,v,i)--Or i,v...


Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 01:50 PM
table.remove() only has two arguments. You have to use the position in the table. You can not name the item, but you have to give the index, such as '1'.

Using two arguments in table.insert() is fine.

I believe that they are added to the end of the table, like that.
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 01:52 PM
Also, you don't use :GetChildren() or :children() on a table.

tablename = {"grimm343","olgRiMM54","Isdjk"}

for _,v in pairs(tablename) do
if v.Name:match("grimm") then
table.remove(tablename,_)
end
end


After running that, tablename is now..

tablename = {"Isdjk"}

The first item, in that kind of loop is the iteration. The second is the object.
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
21 Dec 2011 01:56 PM
I see now, thanks.
Report Abuse
Spectrumw is not online. Spectrumw
Joined: 04 Aug 2009
Total Posts: 13510
21 Dec 2011 02:05 PM
And yet no one noticed that PlayerRemoving is an event from the Players service, not from the Player instance.
Report Abuse
grimm343 is not online. grimm343
Joined: 18 Sep 2008
Total Posts: 2796
21 Dec 2011 02:35 PM
I may not have yelled it out, but I did use it correctly, in my script. :o
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image