Why my loop only occurs twice and the same items are chosen?

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

What’s the point of putting ‘return’ inside the loops? This stops the iteration completely so the loops make no sense.

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.
image

Slightly confused…

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.

@Humanagon The break prevents duplicates.

I have already given you something similiar. The topping selection algorithm can also be used to work in this case. Consistently get random selections - #2 by Kacper

@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. :joy:

It is a logical error. Analyze the code and see that…:

  • the first iteration, nothing prints, index inserted
  • the second iteration, first index printed, second index inserted
  • the third iteration, two index printed, third index inserted(not printed)

Ohhhhh so it’s because nothing is inserted before it prints, so it just prints nil?

No. That’s because the print function is not inside the second block of elseif statement. If there was nil, it would’ve been printed nil.

Ok I think I see what you mean. You’re saying that there should of been a print here?
image

EDIT: Ok if this isn’t it then I have no idea what you’re trying to hint at.

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

Visit the Pizzera!

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