Code generation - called the function

What do you want to achieve?
I would like to receive the code contained in the generated generateCode(), I am trying to use a variable for this, but it does not work as I would like, i.e. I generate the same number (screenshot)

local id = {}

function genCode()
	local tempcode = math.random(1,999)
	if table.find(id, tempcode) then
		task.wait()
		genCode()
	else
		table.insert(id, tempcode)
		
		return tempcode
	end
end

local code = genCode()

image

You are printing the same variable multiple times, it will be always the same until you set the “code” variable to something else

1 Like

in this case it should print the same

print(code)
print(genCode())

It won’t. You already called the function in the code variable once and that variable has been set to the value that has been returned by the function.
That’s why when you print it, it shows you the same value over and over again.

When you call the function like print(genCode()) however, it prints out a new one since you’re calling the function again.

2 Likes

If you want to achieve the same codes, just get rid of the getCode() and the codes will be the same.

Ok, now I understand why this is happening, thanks for your help

2 Likes

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