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: How would I check if a value is already in a table?

Previous Thread :: Next Thread 
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 08:38 PM
Hi, I'm making a queue system but my problem is when a player is already in the queue, how do i make sure he doesnt click again so hes just adding elements in the table when hes already placed in it?


que = {}

script.Parent.MouseClick:connect(function(clicker)
table.insert(que, clicker.Name)
print(#que .. " are currently queue'd")
end)
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
28 Sep 2014 08:44 PM
que is not how you correctly spell queue first of all.

Anyways, this is how you would check to find if the player is in the queue already:

--Onclickystuff
for i, v in pairs(que) do
if v == click.Name then
inqueue = true
break
end
end

if inqueue == false then
--add to queu
end
end) --end onclicky stuff
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 08:47 PM
"Hi, I'm making a queue system but my problem is when a player is already in the queue, how do i make sure he doesnt click again so hes just adding elements in the table when hes already placed in it?"

I think I know how to spell queue in the first sentence of my question. I know que is not the same thing as queue. However q and queue are said the same way.
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 08:48 PM
thanks anyway
Report Abuse
Cawlonee is not online. Cawlonee
Joined: 03 Mar 2014
Total Posts: 2687
28 Sep 2014 08:50 PM
script.Parent.MouseClick:connect(function(clicker)

alreadyin = false
for i=1,#que do
if que[i] == clicker.Name then
alreadyin = true
end
end

if alreadyin ~= true then
table.insert(que, clicker.Name)
print(#que .. " are currently queue'd")
else
print("Was in already")
end
end)
Report Abuse
lordrambo is not online. lordrambo
Joined: 16 Jun 2009
Total Posts: 20628
28 Sep 2014 08:53 PM
Use camelCase
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 08:54 PM
Hey again, I made this script and I saw your post cowl but I want to learn what I did wrong here.

script.Parent.MouseClick:connect(function(clicker)
for i,v in pairs(que) do
if v == clicker.Name then
print("not added because already q'd")
else
table.insert(que, clicker.Name)
end
end
end)

nothing happens, nothing in output.
Report Abuse
Cawlonee is not online. Cawlonee
Joined: 03 Mar 2014
Total Posts: 2687
28 Sep 2014 09:02 PM
How a for loop works, it runs through for every value in the table (or whatever amount of times you give it.)

Lets say, this is my table:

testab = { "hi"; "bye" }

for i,v in pairs(testab) do
if v == "hi" then
print("Don't add")
else
print("Added")
-- Add code here
end
end

It doesn't add the first time around, but then it goes back through with the string "bye", and because "bye" isn't the same as "hi" it adds it.

That's why I made a variable. It loops through and the second it finds a duplicate, it notes it and finished the loop. Then checks if it found the duplicate after it finished.
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 09:09 PM
lets say a table has nothing in it, will the iteration run endlessly?

I tested your's and it works but why doesnt mine work? Are you suggesting I add the value which the players name, and then check if the value is already there then remove it ?
Report Abuse
Cawlonee is not online. Cawlonee
Joined: 03 Mar 2014
Total Posts: 2687
28 Sep 2014 09:15 PM
The for loop won't run at all if it nothing is in it.

An in pairs loop will run once for every value in the table.

You can't check if it's been added from within, or else you'll get a different result each time a new string comes through.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
28 Sep 2014 09:22 PM
You're better off using a function, because otherwise you'll get something like this:


local t = { "A", "B", "C" }

for _, v in pairs(t) do
if v == "C" then
-- don't add
print("Don't add C")
else
-- add
table.insert(t, "C")
end
end

--> t = { "A", "B", "C", "C", "C" }


With a function, you can simply return 'true' if the value is found, otherwise return false at the end of the function. This ensures that the entire table will be traversed before taking any action.



function isInTable(tab, val)
for _, v in pairs(tab) do
if v == val then return true
end
return false
end

local t = { "A", "B", "C" }

if isInTable(t, "C") then
--don't add
print("Don't add")
else
--add
table.insert(t, "C")
end
Report Abuse
Cawlonee is not online. Cawlonee
Joined: 03 Mar 2014
Total Posts: 2687
28 Sep 2014 09:25 PM
Haha Firefox, Don't blow his mind. :p

Either will work, he just wanted to know why his loop didn't.
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 09:27 PM
so I tested this, and it worked but I have doubts about this. Any ideas on what could happen if this were to be put as the main script?

script.Parent.MouseClick:connect(function(clicker)
if #que == 0 then
table.insert(que, clicker.Name)
else
for i, v in pairs(que)do
if v == clicker.Name then
print("Not added")
else
table.Insert(que,clicker.Name)
print("Added")
end
end
end
end)
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 09:28 PM
I used to be a little better before but I've been gone for a couple months and everything is kinda confusing to me right now but I did get what you did firefox.
Report Abuse
Cawlonee is not online. Cawlonee
Joined: 03 Mar 2014
Total Posts: 2687
28 Sep 2014 09:28 PM
That will not work.

The problem isn't that there is no values in the table, it's that you're preternaturally adding from within the loop.

Firefox explained why that won't work.

Use his function or my if statement.
Report Abuse
yurhomi10 is not online. yurhomi10
Joined: 10 Dec 2008
Total Posts: 13886
28 Sep 2014 09:39 PM
function iterate(tab, val)
for i, v in pairs(tab) do
if v == val then print("true")
return true
end
end
print("false") return false
end

script.Parent.MouseClick:connect(function(clicker)
if iterate(que, clicker.Name) == false then
table.insert(que, clicker.Name)
end
end)

I think this works now, thanks for the help.
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