You can already see the print function here. It’s all the way up there. Then we have the second block with no print function called. Therefore you could add another print there. Although it is more of a logic error and you should move the print function down before return.
Where do we relocate the first print to?
As aforementioned, you should relocate it outside of the function or down after insertion.
Ohhhhh so it’s because nothing is inserted before it prints, so it just prints nil?
Oh ok so I was half right. Hey that’s better than I usually do! xd
Not exactly sure how much what was exchanged here pertained to my script, but it still doesn’t really work
local function chooseRandom(dictionary)
local List = {}
for i, v in pairs(dictionary) do
List[#List + 1] = {Key = i, Value = v}
end
if #List == 0 then return end
return List[math.random(#List)]
end
function TaskService:AssignTask(player)
if PlayerTasks[player.UserId] then
if #PlayerTasks[player.UserId] >= 3 then return end
for i, v in pairs(TaskData) do
for _, currentTask in pairs(PlayerTasks[player.UserId]) do
if i == currentTask then break end
table.insert(PlayerTasks[player.UserId], math.random(#PlayerTasks[player.UserId]), i)
print(i)
UpdateTasks:FireClient(player, i)
return
end
end
else
PlayerTasks[player.UserId] = {}
local RandomTask = chooseRandom(TaskData)
table.insert(PlayerTasks[player.UserId], RandomTask.Key)
print(RandomTask.Key)
UpdateTasks:FireClient(player, RandomTask.Key)
end
end
if I print the items selected
Test Task 5
Get two of the same task, and whenever I test it doesn’t seem too random. I did around 20-30 tests, and Visit the Pizzera! and Test Task 5 came out every single time. There’s 10 different tasks in the table
Maybe try the ~= instead of == method? xd
if i ~= currentTask then break end
That would make it only add an item if it’s already in the table, which is not what I want. It should only add the item if it’s NOT already in the players table
You sort of need to sort out the issue of avoiding duplicates through removal from the original table. Create a second table with available tasks, and remove from the dictionary after addition. Perhaps that’ll work.
Visually, you will have three tables. One with all tasks. One with player’s available tasks. One with player’s tasks.
Well if you think that won’t work, did you ever try replacing the break with return? I believe breaks are supposed to break the entire loop and stop it from going.
So I’d have to have 2 tables containing basically the same info?
return {
['Visit the pizzeria!'] = {Coins = 10, Exp = 8},
['Play the pizzeria minigame!'] = {Coins = 5, Exp = 5},
['Buy some furniture!'] = {Coins = 5, Exp = 5},
['Test Task 1'] = {Coins = 1, Exp = 1},
['Test Task 2'] = {Coins = 1, Exp = 1},
['Test Task 3'] = {Coins = 1, Exp = 1},
['Test Task 4'] = {Coins = 1, Exp = 1},
['Test Task 5'] = {Coins = 1, Exp = 1},
['Test Task 6'] = {Coins = 1, Exp = 1},
['Test Task 7'] = {Coins = 1, Exp = 1},
}
-- and
Tasks = {
'Visit the pizzeria!',
'Play the pizzeria minigame!',
-- etc
)
return would cancel the first loop as well. So then the player might only get 1 or 2 tasks, instead of the 3. Basically whenever the function is called, they have to be given a task. Break only breaks the current looping checking to see if they already have that task, once that loop breaks, it moves onto the next task in the list.
Yeah, something like that I would say.
That seems very tedious to have two seperate tables basically containing the exact same information
If the goal here is to have a table with numerical keys:
local taskTable = {
{ -- index 1
"Visit the pizzeria", -- taskTable[1][1] == the task
{Coins = 10, Exp = 8} -- taskTable[1][2] == task rewards
},
{ -- index 2
"Play the pizzeria minigame!",
{Coins = 5, Exp = 5}
},
{ -- index 3
"Buy some furniture!",
{Coins = 5, Exp = 5}
},
}
The goal is to just have it pick a random option
That table actually works too, you can just use math.random()
on that table, because it’s an array. Therefore you should check the task indexed within the player’s task. Only that I fear the use of looping until you find a non-duplicate.
Yeah, a table with numerical indices would be easier to meet your goals.
local task = taskTable[math.random(1, #taskTable)]
To make sure you don’t get a duplicate task, you would just have to generate a random number not equal to the previous number.