Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to have a function that makes sure folder names are different?

In a battle system I'm working on, each battle has a folder with the amount of players fighting and the players fighting, and the amount of enemies and the enemy object values. In a name generator function, the funciton generates a name for the folder to be called. To make sure each of these folder names are different to seperate them, it checks if the number it randomly chose is the same as to any of the folder names. However, the way I'm doing it doesn't work. What am I doing wrong?

function BattleScript.NameGenerator()
	local number = math.random(100)
	local stringnumber = tostring(number)
	print(stringnumber)
	
	for battles in game.ServerStorage.Battles do
		if battles.Name == stringnumber then
			BattleScript.NameGenerator()
		else
			return stringnumber	
		end
	end


	
end

Thanks!

1 answer

I'm not expert, but can you put the same function inside a function? you say this

function BattleScript.NameGenerator()

and inside the function you put this

if battles.Name == stringnumber then
BattleScript.NameGenerator()

1 comment

0

Yes, this is called recursion. The function will call itself. Which can cause some sort of infinite loop if not done properly. Though such a loop usually ends up in a crash as jumping into a function uses up stack memory for the return address and local variables.

This discussion is preserved as it was. New answers, comments, and votes are closed.