So I am trying to make a daily quest system in my game, and I’m starting to make the game choose what challenges the player gets. I wanted to test this first, but it won’t work. I tried printing the value but it just gave me the error “invalid argument #2 to ‘random’ (interval is empty).”
local selectChallenge = math.random(1, #challenges)
The text is for each challenge they get and each number is to select the amount of money they will earn from the challenge. Let me know how I can fix this issue.
#challenges doesn’t work here because you’re using a dictionary that has keys not numbers. You have to create a separate table of all your keys. Then choose a random one that way. After that then you can print.
local challenges = {
["Get 30 Kills"] = {10, 50},
["Play for 20 minutes"] = {25, 50},
["Get 10 Headshots"] = {10, 50},
["Get Top 3"] = {50, 75},
["Walk 500 Studs"] = {10, 50},
["Play 10 Rounds"] = {15, 60},
}
local keys = { "Get 30 Kills","Play for 20 minutes","Get 10 Headshots","Get Top 3","Walk 500 Studs","Play 10 Rounds" }
local randomKey = keys[math.random(1,#keys)]
local challenge_values = challenges[randomKey]
print(randomKey ..": ".. challenge_values[1],challenge_values[2])
I think you could definitely do this, but I would NOT recommend this method. I would switch the items around. So the challenges table would look like this:
local challenges = {
[10] = {"Get 30 Kills", "Get 10 Headshots", "Walk 500 Studs"}
[50] = {"Get Top 3", "Get 30 Kills", "Play for 20 minutes", "Get 10 Headshots", "Walk 500 Studs", "Play 10 Rounds" }
[15] = {"Play 10 Rounds"}
[60] = {"Play 10 Rounds"}
[25] = {"Play for 20 minutes"}
[75] = {"Get Top 3"}
}
-- hopefully I did it right
This would work, but I don’t know what those values represented in his original table. If he wants to keep a similar format to his original code he could simply make a mixed table that looks similar to what you suggested
You have a good point, but I always prioritize speed over the format of code. Of course not for it to be messy, and unreadable but speed. He said he wants to make a daily quest system, so I’m guessing those numbers meant coins, or days.