Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
0
Answered by
iiKind 13
7 years ago

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()
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. Link150 1355 — 7y
Ad

Answer this question