Hello guys, im having a problem, I want to take a random unit in a folder but I want to check if the unit selected has a stringvalue if not then the script will roll until a unit that got the stringvalue, idk if im clear, but I have no idea how to do it. I searched already on the devforum and found this `local
function RefreshBanner(banner)
if banner == "Normal" then
local Units = ReplicatedStorage.UnitsBanner[banner]:GetChildren()
local randomUnit = Units[math.random(3, #Units)]
print(randomUnit)
end
end
but it print only one unit so idk, if you guys can help me, i’ll really appreciate it
function RefreshBanner(banner)
if banner == "Normal" then
local Units = ReplicatedStorage.UnitsBanner[banner]:GetChildren()
local useableUnits = {}
for _,v in Units do
if --[[condition here]] then
table.insert(useableUnits, v)
end
end
local randomUnit = useableUnits[math.random(3, #useableUnits)]
print(randomUnit)
end
end
why is the first parameter for your math.random statement 3? you’re essentially skipping over the first 2 children in the folder, so if your UnitsBanner folder only has 3 units in it, then that’s why it’s returning the same unit every time. try replacing it with:
local randomUnit = useableUnits[math.random(1, #useableUnits)]
oh, ok ty now im having another problem, I want to remove the unit chosen from the table, but it return an Instance and I need a number
now the function look like this
function RefreshBanner(banner)
if banner == "Normal" then
local Units = ReplicatedStorage.UnitsBanner[banner]:GetChildren()
local useableUnits = {}
for _, unit in Units do
if unit:FindFirstChild("Rarity") then
local UnitRarity = unit:FindFirstChild("Rarity").Value
if UnitRarity == "Mythic" then
table.insert(useableUnits, unit)
end
end
end
print(useableUnits)
local randomUnit1 = useableUnits[math.random(1, #useableUnits)]
table.remove(useableUnits, randomUnit1)
local randomUnit2 = useableUnits[math.random(1, #useableUnits)]
table.remove(useableUnits, randomUnit2)
local randomUnit3 = useableUnits[math.random(1, #useableUnits)]
table.remove(useableUnits, randomUnit3)
end
end
Would something like this work? it selects random unit until its condition is true
function GetRandomUnit(Units)
local randomUnit = Units[math.random(1, #Units)]
-- Check the condition
local Condition = false -- If doesn't have the required condition
if not condition then
return GetRandomUnit(Units)
else
return RandomUnit
end
end
function RefreshBanner(banner)
if banner == "Normal" then
local Units = ReplicatedStorage.UnitsBanner[banner]:GetChildren()
local RandomUnit = GetRandomUnit(Units)
end
end
uh i wouldn’t call that optimized but if it works then fine, the one i made loops thru the units until it suits the conditions and doesn’t give the same each time as its completely random
Hey, rather than doing math.random(1, #useableUnits) and then using table.find(useableUnits, randomUnit3)
You can just do
local randomUnitIndex = math.random(1, #useableUnits)
local randomUnit = useableUnits[randomUnitIndex]
table.remove(useableUnits, randomUnitIndex)
You can also use a for loop to make your code shorter
local unitsChosen = {}
for i = 1, 3 do
local randomUnitIndex = math.random(1, #useableUnits)
table.insert(unitsChosen, useableUnits[randomUnitIndex])
table.remove(useableUnits, randomUnitIndex)
end
Now the first unit is unitsChosen[1] second is unitsChosen[2] and so on