You should somehow shuffle the dictionary, which could be taking only an extra step to do so. Alternatively, shuffling the index choices. You will need:
Strings in an array
Randomizing a pick from the array
Getting the task information after duplicate checks
I think it might be because you’re breaking it when it gets to the current one? It’s just a guess but wouldn’t want to leave any possibility unmentioned.
EDIT: I’m saying, maybe its because of this, sorry for any confusion.
local TaskArray = {
"Visit the Pizzeria!"; "Play the pizzeria minigame!"; "Buy some furnitures!"
}
local function shufflePick(t)
return TaskData[TaskArray[math.random(#TaskArray)]]
end
The practice goes something along the lines of this.
@Operatik I just looked at for quite a bit. I see what you’re saying but if it was to prevent duplicates, wouldn’t it be ~= instead of ==? I’m a relatively new scripter so correct me if I’m wrong, but to me it looks like the reason it’s printing the two and not the three is because it’s breaking when its on the current task, not when it’s on the others. Maybe that’s why it only prints the 2, because it does 2 at first, then when it gets to the last one, it just breaks because it’s the current one and doesn’t print?
Unfortunately, it should be ==, because you’re matching the pre-made information with the created information. If there are matches, it would not insert the new information.
Also it is irrelevant about the printing currently. As aforementioned, I have clarified that there was only two prints and both prints indicate the contents before insertion.
So if it’s not the break, and it’s not an incorrect symbol, what’s making it stop? I’m looking at it from every angle possible and I can’t figure out anything else.
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.
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
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
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