Announcement

Collapse
No announcement yet.

Only picking Array 1 time.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Only picking Array 1 time.

    Hello I'm trying make some cards with lua and have made it someway now. Drawing cards from a deck is random and works fine aswell as displays fine.

    Yet I want to be able to only draw the cards 1 time. untill the deck has been shuffled again. So I need to somehow limit how many times I can draw the cards. and a command to shuffle them again.
    Code looks like this so far.

    card={"♥2", "♥3", "♥4", "♥5", "♥6", "♥7", "♥8", "♥9", "♥10", "♥J", "♥Q", "♥K", "♥A", "♣2", "♣3", "♣4", "♣5", "♣6", "♣7", "♣8", "♣9", "♣10", "♣J", "♣Q", "♣K", "♣A", "♦2", "♦3", "♦4", "♦5", "♦6", "♦7", "♦8", "♦9", "♦10", "♦J", "♦Q", "♦", "♦ACE", "♠2", "♠3", "♠4", "♠5", "♠6", "♠7", "♠8", "♠9", "♠10", "♠J", "♠Q", "♠K", "♠A"}

    function cmd_card (player)
    math.randomseed( os.time() )
    AddPlayerChat(player, "drawed " .. (card[math.random(#card)]))
    end
    function cmd_card2(player)
    math.randomseed( os.time() )
    AddPlayerChat(player, "drawed " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]))
    end
    function cmd_card3(player)
    math.randomseed( os.time() )
    AddPlayerChat(player, "drawed " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]))
    end
    function cmd_card4(player)
    math.randomseed( os.time() )
    AddPlayerChat(player, "drawed " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]))
    end
    function cmd_card5(player)
    math.randomseed( os.time() )
    AddPlayerChat(player, "drawed " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]) .. " - " .. (card[math.random(#card)]))
    end

    AddCommand("card", cmd_card)
    AddCommand("card2", cmd_card2)
    AddCommand("card3", cmd_card3)
    AddCommand("card4", cmd_card4)
    AddCommand("card5", cmd_card5)

    Thanks in advance if someone is able to help me.

    #2
    You can use something called a "Debounce" to determin which player has a turn or not, a good way to do this is put all the players who are playing, or all players on the server into a table. In this case i'm going to put them all into one table.

    Code:
    local Players = {}
    function InitializeTable() -- Let's put all the players currently online into one big useable table
          for _,v in ipairs(GetAllPlayers()) do
              if IsValidPlayer(v) then
                  Players[GetPlayerName(v)] = false
              end
          end
    end
    
    InitializeTable()
    
    function PlayerDisconnected(ply) -- if a player leaves, let's not waste memory let's remove the player
          if Players[ply] or not Players[ply] then -- Making sure that "ply" exists in the table, whether or not its true or false
              Players[ply] = nil
          end
    end
    AddEvent("OnPlayerQuit", PlayerDisconnected)
    
    -- Function to use as a condition if the player can draw a card or not
     -- Usage: if CheckValidPlayer(PlayersFullName)
    function CheckValidPlayer(ply)
          if Players[ply] then
              return true
          end
          return false
    end
     -- To set a player to be able to draw a card or not Usage: Players["sl0th"] = true / false

    This code may have issues, I coded it blind but this is the kind of thing that you could use. There may be a better way but I made this up on the spot

    Comment


      #3
      I'm so new at Lua that i went through your code 10 times now and only understand 10% but I'm getting closer and I much appreciate your answer.

      I see I need to make a function that checks if that card has been drawn (if the array has been printed once) before I actually show the card.

      function cmd_card (player)
      math.randomseed( os.time() )
      AddPlayerChat(player, "drawed " .. (card[math.random(#card)]))

      the math.random should be changed with a value that i get from another function that does the math.random and checks the card when drawn or discards if allready drawn?`

      Comment

      Working...
      X