Making sure random does not equal the last random

I am trying to make the randomly assigned number doesn’t equal the last random number it chose

My script isn’t working and I’ve tried multiple solutions.

The code that I use right now was found on the dev forum, I tried to implement it but it still does not work – I have no clue why


local CurrentEquipped = plr.Character:WaitForChild("CurrentEquipped")
	
	--
	local random = Random.new()
	local randomIndex
	
	--
	while not randomIndex or randomIndex == LastRandomGun or randomIndex == CurrentEquipped.Value do
		randomIndex = random:NextInteger(1, #DisplayGuns)
	end
	
	--
	LastRandomGun = randomIndex
	
	--
	return DisplayGuns[randomIndex]	

I don’t completely understand what you’re trying to do here and your code is missing some variable declarations, it would be a lot easier if you could provide full code. Starting from what the title says, you can use table.remove() to prevent the object to be selected again. Ex:

local Seed = Random.new()

local SS = game:GetService("ServerStorage")
local GunsTable = SS.Guns:GetChildren()

local RandomNumber = Seed:NextInteger(1, #GunsTable)
local RandomGun = GunsTable[RandomNumber]

print(RandomGun)
table.remove(GunsTable, RandomNumber)
print(GunsTable[RandomNumber])

image
image

1 Like
local randLetters = {"a", "b", "c", "d"}
local selectedRand = {}

local randSelected = randLetters[math.random(1, #randLetters)]
if selectedRand[randSelected] then
	print(randSelected, " has already been selected.")
else
	selectedRand[#selectedRand+1] = randSelected
	print(randSelected, " was randomly selected.")
end

Is this the kind of logic you’re looking for?

1 Like

What Im trying to do is make sure when a random gun is selected, it doesnt give you one it already has.

local DisplayGuns = {}
local BlacklistedGuns = {}

local SpinDebounce = false

local LastRandomGun = nil
local CurrentDisplay = nil
local RandomGun = nil

-- FUNCS
local function getRandomDisplayGun(plr)
	local CurrentEquipped = plr.Character:WaitForChild("CurrentEquipped")
	
	--
	local random = Random.new()
	local randomIndex
	
	--
	while not randomIndex or randomIndex == LastRandomGun or randomIndex == CurrentEquipped.Value do
		randomIndex = random:NextInteger(1, #DisplayGuns)
	end
	
	--
	LastRandomGun = randomIndex
	
	--
	return DisplayGuns[randomIndex]	
	
end

Im trying to make a call of duty mystery box effect