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 » Scripters
Home Search
 

Re: Prevent duplicates in a table?

Previous Thread :: Next Thread 
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 11:20 AM
for some odd readon I can't get this to work right..can you show me what I'm doing wrong?

if not _G.plrs[plr] then
print(plr)
table.insert(_G.plrs,plr)
end

Output:
>
Player1
Player1
Player1

The event fires when I click a TextButton and keeps printing even if there is already an object in it..do tables already prevent duplicates?
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 11:21 AM
matchset.OnServerEvent:connect(function(plr)
if not _G.plrs[plr] then
print(plr)
table.insert(_G.plrs,plr)
end
end)
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 11:35 AM
Ah. Simple fix.

matchset.OnServerEvent:connect(function(plr)
if #_G.plrs > 0 then
for i, v in next, (_G.plrs) do
if not plr.Name == v then
table.insert(_G.plrs,plr)
else
print("Already in table")
end
end
else
table.insert(_G.plrs,plr)
end
end)
Report Abuse
OzzyFin is not online. OzzyFin
Joined: 07 Jun 2011
Total Posts: 3600
17 Jun 2015 11:35 AM
function findfromtable(t,val)
for _,v in pairs(t) do
if v == val then
return true
end
end
return false -- idk is it needed ive always put it there
end

if not findfromtable(_G.plrs,plr) then

end

Report Abuse
OzzyFin is not online. OzzyFin
Joined: 07 Jun 2011
Total Posts: 3600
17 Jun 2015 11:36 AM
wow u ninjad me urself

ok i was slow i understand
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 11:38 AM
ahaha Sorry xD
Report Abuse
Dr01d3k4 is not online. Dr01d3k4
Joined: 11 Oct 2007
Total Posts: 17916
17 Jun 2015 11:48 AM
Change it to a hash table (dictionary) instead of an array.
Instead of table.insert(_G.players, player)
Use _G.players[player] = true

Hash tables have an average-case lookup and insertion complexity of O(1).
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 12:05 PM
This is the error I get when I try that:

13:01:40.294 - keys must be strings

--server script
function matches.OnServerInvoke()
return _G.plrs
end

matchset.OnServerEvent:connect(function(plr)
if #_G.plrs > 0 then
for i, v in next, (_G.plrs) do
if plr.Name ~= v then
--workspace.Bool.Value = true
--table.insert(_G.plrs,plr)
_G.plrs[plr] = true
else
workspace.Bool.Value = false
print(plr.Name)
print("Already in table")
end
end
else
--workspace.Bool.Value = true
--table.insert(_G.plrs,plr)
_G.plrs[plr] = true
end
end)

--local script
local plrs = matches:InvokeServer() --error

Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 12:11 PM
lol wtf now my game is crashing when i use this code:


--server script

_G.plrs = game.Players:GetPlayers()

function matches.OnServerInvoke()
return _G.plrs
end

matchset.OnServerEvent:connect(function(plr)
if #_G.plrs > 0 then
for i, v in next, (_G.plrs) do
if plr.Name ~= v then
table.insert(_G.plrs,plr)
else
print(plr.Name) --started crashing when i added this i think?
print("Already in table")
end
end
else
table.insert(_G.plrs,plr)
end
end)

--local script
while wait(1) do
local plrs = matches:InvokeServer()
loadPlrs(plrs)
end
Report Abuse
iiEssence is not online. iiEssence
Joined: 18 Jun 2014
Total Posts: 3467
17 Jun 2015 12:12 PM
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
wiki.roblox.com
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
17 Jun 2015 12:13 PM
@iiEssence You're an idiot.
Report Abuse
AntiFiter is not online. AntiFiter
Joined: 14 May 2009
Total Posts: 12290
17 Jun 2015 12:14 PM
http://wiki.roblox.com/index.php?title=Basic_math
Report Abuse
iiEssence is not online. iiEssence
Joined: 18 Jun 2014
Total Posts: 3467
17 Jun 2015 12:15 PM
OHH IM SORRY

http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
Report Abuse
masterblokz is not online. masterblokz
Joined: 17 Nov 2010
Total Posts: 9517
17 Jun 2015 12:17 PM
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting

Report Abuse
AntiFiter is not online. AntiFiter
Joined: 14 May 2009
Total Posts: 12290
17 Jun 2015 12:18 PM
eww global variables
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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