Ensuring different location afterwards

Hey! In my script, I have a function, which randomly picks 1 location from 20 (parts).

local currentEggLocation
local LocationsEggs = workspace:WaitForChild("EasterEggsLocations")

local function getRandomEggLocation()
	local children = LocationsEggs:GetChildren()
	currentEggLocation = children[math.random(1, #children)]
end

How can I ensure so that it doesn’t choose the exactly same location right afterwards? In other words, location1 shouldn’t be picked after location1, but location1 can be picked again after location1 and location2 were picked.

This might help you out:
How to make Table reset when all numbers are picked?

1 Like

Just use one table, and tracking the last or previous location.

local currentEggLocation
local LocationsEggs = workspace:WaitForChild("EasterEggsLocations")
local availableEggLocations = LocationsEggs:GetChildren()
local previousLocation, previousIndex

local function getRandomEggLocation()
	local randomNumber = math.random(#availableEggLocations)
	currentEggLocation = availableEggLocations[randomNumber]

	table.remove(availableEggLocations, randomNumber)
	if previousLocation then
		table.insert(availableEggLocations, previousLocation, previousIndex)
	end

	previousLocation = currentEggLocation
	previousIndex = randomNumber
end
2 Likes

Thanks! Though, there is one issue. It returns this error:


Any idea how to fix it?

needs to be changed to:

table.insert(availableEggLocations, previousIndex, previousLocation);

he just switched some arguments but the order is array, position, value

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.