How to make something not pick the same thing twice

GENERATE STOP (main code)

function mainModule.GenerateStop()
	local possibleStops = RSStopStorage:GetChildren()
	local randomStop = possibleStops[math.random(1, #possibleStops)]
	local newStop = randomStop:Clone()
	
	newStop.Parent = WSStopStorage
	
	mainModule.TweenGlass(1, MainSubway.LeftDoor.Glass, Color3.fromRGB(152, 194, 219), 0.75)
	mainModule.TweenGlass(1, MainSubway.RightDoor.Glass, Color3.fromRGB(152, 194, 219), 0.75)
	task.wait(1)
	mainModule.TweenDoors(1, MainSubway.LeftDoorOutDestination, MainSubway.LeftDoor.Door)
	mainModule.TweenDoors(1, MainSubway.RightDoorOutDestination, MainSubway.RightDoor.Door)
		
	Values.Stops.Value += 1
	Values.AtStop.Value = true
end

DESTROY THE CHILDREN / THE LOOP

BE.EndedStop.Event:Connect(function()
	MainModule.TweenDoors(1, MainSubway.LeftDoorInDestination, MainSubway.LeftDoor.Door)
	MainModule.TweenDoors(1, MainSubway.RightDoorInDestination, MainSubway.RightDoor.Door)
	task.wait(1)
	MainModule.TweenGlass(1, MainSubway.LeftDoor.Glass, Color3.fromRGB(18, 28, 35), 0)
	MainModule.TweenGlass(1, MainSubway.RightDoor.Glass, Color3.fromRGB(18, 28, 35), 0)
	
	task.wait(1)
	WSStopStorage:ClearAllChildren()
	
	for i = 5, 0, -1 do
		MainSubway.TimeTillStop.MainText.SurfaceGui.TextLabel.Text = tostring(i)
		task.wait(1)
	end
	
	MainModule.GenerateStop()
end)

I want to make it so that when randomStop gets chosen, I don’t want the chance of it being the previous stop.
(hopefully that makes sense)

Any help is appreciated!

What about this? At the beginning newStop is still the same value as the previous event, right?
Instead of:
local newStop = randomStop:Clone()
do

if newStop == randomStop then  --checks to see if the old value and the new one are the same
    return end
else 
    newStop = randomStop:Clone()
    --do the rest of the code you had before and add an end to close this if statement.
1 Like

Simply remove the index using table.remove

table.remove(possibleStops, randomStop)
1 Like

Could you demonstrait that for me?

This didn’t work :confused:

Thank you for the suggestion though!

For example, lets say I have this table, and I pick a random number from it:

local numbers = {1, 2, 3, 4, 5}

local randomNum = numbers[math.random(1, #numbers)]

When the number is chosen, I remove the index so the next time I use it, I wont have to worry about it being chosen again:

table.remove(numbers, randomNum)

Lets say the random number is 3, this is what our table would look like:

numbers = {1, 2, 4, 5}

Usually this only works for Arrays, but since GetChildren returns an Array, it should work as intended.

1 Like

But wouldn’t that make it so that it will never be chosen again?
I want it to still be able to choose, just not right after it just was.

Or since it’s a function would the table reset every time the loop calls it?

Every time the function is called, it should still return all the children, if you are using a loop and dont want a number to be chosen again, this would be the way to go.

1 Like

I actually got it myself, by adding a string value in workspace

function mainModule.GenerateStop()
	local possibleStops = RSStopStorage:GetChildren()
	local randomStop = possibleStops[math.random(1, #possibleStops)]
	
	if Values.PreviousStop.Value == randomStop.Name then
		return mainModule.GenerateStop()
	end	

	local newStop = randomStop:Clone()
	newStop.Parent = WSStopStorage
	
	Values.PreviousStop.Value = newStop.Name
	
	mainModule.TweenGlass(1, MainSubway.LeftDoor.Glass, Color3.fromRGB(152, 194, 219), 0.75)
	mainModule.TweenGlass(1, MainSubway.RightDoor.Glass, Color3.fromRGB(152, 194, 219), 0.75)
	task.wait(1)
	mainModule.TweenDoors(1, MainSubway.LeftDoorOutDestination, MainSubway.LeftDoor.Door)
	mainModule.TweenDoors(1, MainSubway.RightDoorOutDestination, MainSubway.RightDoor.Door)
		
	Values.Stops.Value += 1
	Values.AtStop.Value = true
end

Thank you guys for trying to help though!

Sorry, I should have added another line to that. Because if newStop has a value it’ll be because the last time the function ran the newStop value is still saved.

if newStop == randomStop then  --checks to see if the old value and the new one are the same
    -- call the function again
else 
    newStop = randomStop:Clone()
    --do the rest of the code you had before and add an end to close this if statement.

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