Damien_Teh_Demon Posted June 22, 2014 Share Posted June 22, 2014 Im trying to check a lua table to see if 3 of the values in the table are the same. The table consists of strings and looks like this possibiltyThings = {"2k","4k","6k","8k","10k","12k","14k","16k", "18k","20k","40k","80k"} Without using tons of if statements. Link to comment
arezu Posted June 22, 2014 Share Posted June 22, 2014 The first solution I think of is: local values = {"2k","4k","6k","8k","10k","12k","14k","16k", "18k","20k","40k","80k"} local sameValueCount = {} for k, v in pairs(values) do sameValueCount[v] = (sameValueCount[v] or 0) + 1 if(sameValueCount[v] == 3)then -- 3 same values in table (those 3 have the value 'v') end end Link to comment
Damien_Teh_Demon Posted June 22, 2014 Author Share Posted June 22, 2014 randomNumber1 = possibiltyThings[math.random(#possibiltyThings)] randomNumber2 = possibiltyThings[math.random(#possibiltyThings)] randomNumber3 = possibiltyThings[math.random(#possibiltyThings)] randomNumber4 = possibiltyThings[math.random(#possibiltyThings)] randomNumber5 = possibiltyThings[math.random(#possibiltyThings)] randomNumber6 = possibiltyThings[math.random(#possibiltyThings)] randomNumber7 = possibiltyThings[math.random(#possibiltyThings)] randomNumber8 = possibiltyThings[math.random(#possibiltyThings)] randomNumber9 = possibiltyThings[math.random(#possibiltyThings)] for k, v in pairs(possibiltyThings) do sameValueCount[v] = (sameValueCount[v] or 0) + 1 if(sameValueCount[v] == 3)then amountToGive = split ( v, "k" ) exports.SARGcommands:giveMoney(localPlayer, tonumber(amountToGive)) end end Why wont this work? Link to comment
Moderators Citizen Posted June 22, 2014 Moderators Share Posted June 22, 2014 - splitreturns a table - You needed a table to store the random numbers and then iterate over it to look for 3 same values in it. local randomNumbers = {} for k=1, 9 do table.insert(randomNumbers, possibiltyThings[math.random(#possibiltyThings)]) end --check for 3 same values: local sameValueCount = {} for k, v in ipairs( randomNumbers ) do sameValueCount[v] = (sameValueCount[v] or 0) + 1 if (sameValueCount[v] == 3) then local amountToGive = split ( v, "k" )[1] -- *1000 ? exports.SARGcommands:giveMoney(localPlayer, tonumber(amountToGive)) break --stop looking for other 3 same value, remove it if you want to -- give cash for every 3 same values there are in randomNumbers. end -- reset sameValueCount = {} randomNumbers = {} end Link to comment
Damien_Teh_Demon Posted June 22, 2014 Author Share Posted June 22, 2014 Okayi posted it wrong earlier, what about thiss possibiltyThings = {2,4,6,8,10,12,14,16,18,20} sameValueCount={} for k, v in pairs(possibiltyThings) do sameValueCount[v] = (sameValueCount[v] or 0) + 1 if(sameValueCount[v] == 3)then amountToGive = possibiltyThings[v]*1000 print("test") exports.SARGcommands:giveMoney(localPlayer, amountToGive) end end Link to comment
Damien_Teh_Demon Posted June 23, 2014 Author Share Posted June 23, 2014 It never returns true though. Link to comment
Moderators Citizen Posted June 23, 2014 Moderators Share Posted June 23, 2014 It never returns true though. Of course ! Tell me where do you see 3 times the same value in this list ?! {2,4,6,8,10,12,14,16,18,20} I think your are going wrong, please explain exactly what you want to do. It looks like you are trying to make a lottery or a thing like this (I don't know the english name for that): But I don't understand why you are using the hardcoded list of possibilities as the "randomNumbers". I gave you a code that works and that I'm pretty sure it was what you were looking for. So please explain in details. Link to comment
Damien_Teh_Demon Posted June 23, 2014 Author Share Posted June 23, 2014 No, i need it to work sort of like that but random choose the values from that table, here is full code local screenWidth,screenHeight = guiGetScreenSize() GUIEditor = { button = {}, window = {} } possibiltyThings = {2,4,6,8,10,12,14,16,18,20} randomNumbers = {} for k=1, 9 do table.insert(randomNumbers, possibiltyThings[math.random(#possibiltyThings)]) end addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function() GUIEditor.window[1] = guiCreateWindow(207, 166, 224, 167, "SARG:RPG Lottery Panel", false) guiWindowSetSizable(GUIEditor.window[1], false) GUIEditor.button[1] = guiCreateButton(25, 99, 180, 31, "close", false, GUIEditor.window[1]) GUIEditor.button[2] = guiCreateButton(31, 33, 164, 34, "Buy Ticket", false, GUIEditor.window[1]) GUIEditor.button[3] = guiCreateButton(456, 683, 453, 35, "Scratch The Ticket To See If You Win!", false) guiSetVisible(GUIEditor.button[3], false) addEventHandler("onClientGUIClick", GUIEditor.button[3], function() lotteryTickOpen = false lotteryTickScratchedOpen = true exports.SARGcommands:sendClientMessage("*SARG Lottery* Goodluck!", 0, 255, 0) guiSetVisible(GUIEditor.button[3], false) randomNumber1 = possibiltyThings[math.random(#possibiltyThings)] randomNumber2 = possibiltyThings[math.random(#possibiltyThings)] randomNumber3 = possibiltyThings[math.random(#possibiltyThings)] randomNumber4 = possibiltyThings[math.random(#possibiltyThings)] randomNumber5 = possibiltyThings[math.random(#possibiltyThings)] randomNumber6 = possibiltyThings[math.random(#possibiltyThings)] randomNumber7 = possibiltyThings[math.random(#possibiltyThings)] randomNumber8 = possibiltyThings[math.random(#possibiltyThings)] randomNumber9 = possibiltyThings[math.random(#possibiltyThings)] --check for 3 same values: sameValueCount={} for k, v in pairs(possibiltyThings) do sameValueCount[v] = (sameValueCount[v] or 0) + 1 if(sameValueCount[v] == 3)then amountToGive = possibiltyThings[v]*1000 print("test") exports.SARGcommands:giveMoney(v, amountToGive) end end setElementData(localPlayer,"ticketsLeft", getElementData(localPlayer,"ticketsLeft")-1) setTimer(function() showCursor(false) guiSetVisible(GUIEditor.button[3], false) lotteryTickScratchedOpen = false end,5000, 1) end) guiSetVisible(GUIEditor.window[1], false) addEventHandler ( "onClientGUIClick", GUIEditor.button[1], function() guiSetVisible(GUIEditor.window[1], false) showCursor(false ) end ) addEventHandler ( "onClientGUIClick", GUIEditor.button[2], function() if(getElementData(localPlayer,"ticketsLeft"))then setElementData(localPlayer,"ticketsLeft", getElementData(localPlayer,"ticketsLeft")+1) exports.SARGcommands:sendClientMessage("*SARG Lottery* You have bought 1 ticket! Do /lotto to see if you won! ( You have a total of "..getElementData(localPlayer,"ticketsLeft").." tickets now!)", 0, 255, 0) else setElementData(localPlayer,"ticketsLeft",1) end end ) lottoStoreMarker = createMarker(2637.25390625,1129.6761474609,11.1796875,"cylinder",3.0,0,255,0,100) addEventHandler("onClientMarkerHit", lottoStoreMarker, function(hitElement) if(hitElement==localPlayer)then guiSetVisible(GUIEditor.window[1], true) showCursor(true) end end) end) addCommandHandler("lotto", function() lotteryTickOpen = true guiSetVisible(GUIEditor.button[3], true) showCursor(true) end) addEventHandler("onClientRender", getRootElement(), function() if(lotteryTickOpen and getElementData(localPlayer,"ticketsLeft")>0)then dxDrawImage ( screenWidth/2-224.5,screenHeight/2-300, 449, 600, 'lotteryNotscratched.png') elseif(lotteryTickScratchedOpen and getElementData(localPlayer,"ticketsLeft")>0)then dxDrawImage ( screenWidth/2-224.5,screenHeight/2-300, 449, 600, 'lotteryScratched.png') --lotoslots dxDrawText(tostring(randomNumber1), 467, 290, 584, 402, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber2), 613, 290, 730, 402, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber3), 768, 290, 885, 402, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber4), 467, 432, 584, 544, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber5), 618, 432, 735, 544, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber6), 768, 432, 885, 544, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber7), 471, 562, 588, 674, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber8), 622, 562, 739, 674, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumber9), 768, 562, 885, 674, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) end end) -- SLOT 1,1 ----- dxDrawText("1K", 467, 290, 584, 402, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 1,2 ----- dxDrawText("2K", 613, 290, 730, 402, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 1,3 ----- dxDrawText("3K", 768, 290, 885, 402, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 2,1 ----- dxDrawText("4K", 467, 432, 584, 544, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 2,2 ----- dxDrawText("5K", 618, 432, 735, 544, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 2,3 ----- dxDrawText("6K", 768, 432, 885, 544, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 3,1 ----- dxDrawText("7K", 471, 562, 588, 674, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 3,2 ----- dxDrawText("8K", 622, 562, 739, 674, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) -- SLOT 3,3 ----- dxDrawText("9K", 768, 562, 885, 674, tocolor(255, 255, 255, 255), 3.00, "default", "center", "center", false, false, true, false, false) Link to comment
Moderators Citizen Posted June 23, 2014 Moderators Share Posted June 23, 2014 local screenWidth,screenHeight = guiGetScreenSize() GUIEditor = { button = {}, window = {} } local possibiltyThings = {2,4,6,8,10,12,14,16,18,20} addEventHandler("onClientResourceStart", resourceRoot, function() GUIEditor.window[1] = guiCreateWindow(207, 166, 224, 167, "SARG:RPG Lottery Panel", false) guiWindowSetSizable(GUIEditor.window[1], false) GUIEditor.button[1] = guiCreateButton(25, 99, 180, 31, "close", false, GUIEditor.window[1]) GUIEditor.button[2] = guiCreateButton(31, 33, 164, 34, "Buy Ticket", false, GUIEditor.window[1]) GUIEditor.button[3] = guiCreateButton(456, 683, 453, 35, "Scratch The Ticket To See If You Win!", false) guiSetVisible(GUIEditor.window[1], false) guiSetVisible(GUIEditor.button[3], false) -- scratch button addEventHandler("onClientGUIClick", GUIEditor.button[3], function() lotteryTickOpen = false lotteryTickScratchedOpen = true exports.SARGcommands:sendClientMessage("*SARG Lottery* Goodluck!", 0, 255, 0) guiSetVisible(GUIEditor.button[3], false) randomNumbers = {} for k=1, 9 do table.insert(randomNumbers, possibiltyThings[math.random(#possibiltyThings)]) end --check for 3 same values in randomNumbers: sameValueCount = {} local amountToGive = 0 for k, v in pairs(randomNumbers) do sameValueCount[v] = (sameValueCount[v] or 0) + 1 if (sameValueCount[v] == 3) then amountToGive = amountToGive + v*1000 end end if amountToGive > 0 then outputChatBox("You won: $"..amountToGive) exports.SARGcommands:giveMoney(localPlayer, amountToGive) else outputChatBox("Sorry, you lost !") end setElementData(localPlayer, "ticketsLeft", (getElementData(localPlayer,"ticketsLeft") or 0) - 1) setTimer(closeLoteryTicket, 5000, 1) end) -- close button addEventHandler ( "onClientGUIClick", GUIEditor.button[1], function() guiSetVisible(GUIEditor.window[1], false) showCursor(false) end) -- buy button addEventHandler ( "onClientGUIClick", GUIEditor.button[2], function() if(getElementData(localPlayer, "ticketsLeft"))then setElementData(localPlayer, "ticketsLeft", getElementData(localPlayer,"ticketsLeft")+1) exports.SARGcommands:sendClientMessage("*SARG Lottery* You have bought 1 ticket! Do /lotto to see if you won! ( You have a total of "..tostring(getElementData(localPlayer,"ticketsLeft")).." tickets now!)", 0, 255, 0) else setElementData(localPlayer, "ticketsLeft", 1) end end) -- marker local lottoStoreMarker = createMarker(2637.25390625,1129.6761474609,11.1796875,"cylinder",3.0,0,255,0,100) addEventHandler("onClientMarkerHit", lottoStoreMarker, function( hitElement ) if hitElement == localPlayer then guiSetVisible(GUIEditor.window[1], true) showCursor(true) end end) end) addCommandHandler("lotto", function() local tickets = getElementData(localPlayer,"ticketsLeft") or 0 if tickets > 0 then openLoteryTicket() else outputChatBox("You don't have any lottery tickets !") end end) function openLoteryTicket() lotteryTickOpen = true guiSetVisible(GUIEditor.button[3], true) showCursor(true) addEventHandler("onClientRender", root, drawLoteryTicket) end function closeLoteryTicket() lotteryTickOpen = false lotteryTickScratchedOpen = false guiSetVisible(GUIEditor.button[3], false) showCursor(false) removeEventHandler("onClientRender", root, drawLoteryTicket) end function drawLoteryTicket() if lotteryTickOpen then dxDrawImage ( screenWidth/2-224.5,screenHeight/2-300, 449, 600, 'lotteryNotscratched.png') elseif lotteryTickScratchedOpen then dxDrawImage ( screenWidth/2-224.5,screenHeight/2-300, 449, 600, 'lotteryScratched.png') --lotoslots dxDrawText(tostring(randomNumbers[1]), 467, 290, 584, 402, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[2]), 613, 290, 730, 402, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[3]), 768, 290, 885, 402, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[4]), 467, 432, 584, 544, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[5]), 618, 432, 735, 544, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[6]), 768, 432, 885, 544, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[7]), 471, 562, 588, 674, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[8]), 622, 562, 739, 674, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) dxDrawText(tostring(randomNumbers[9]), 768, 562, 885, 674, tocolor(0,0,0, 255), 3.00, "default", "center", "center", false, false, true, false, false) end end Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now