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