Function returns nothing as a string

Hey guys, I’m trying to make a game where it randomly generates a universeId when i press a button. When I press the button, it calls a function called ‘getgame()’, and in the function it correctly prints the universeid with no issue, when I return the string, it still pops up with nothing. I tried looking for answers for this but I can’t find the right one for me.

Here are some of the screenshots:
image_2024-07-23_162331298

Here is the code:

local function getgame()
	local id1,id2 = math.random(0,gameid/2),math.random(0,gameid/2)
	local id = 10 * (id1 + id2) + math.random(0,9)
	local getuniverselink = 'https://apis.roproxy.com/universes/v1/places/'..id..'/universe'
	local und = httpservice:GetAsync(getuniverselink)
	local undLua = string.sub(und,2,string.len(und)-1)
	local undTable = string.split(undLua,':')
	local uniId = undTable[2]
	
	if uniId == 'null' or nil then
		getgame()
	else
		print(uniId)
		return tonumber(uniId)
	end
		
end
rollevent.OnServerEvent:Connect(function(plr)
	print(getgame())
end)

If anyone can help, that would be great!

2 Likes

I think it might be because uniId is ‘null’ or nil. This causes the fucntion to not return anything, which makes the print print nothing.

To solve this, you could make it repeat the code until it does not return ‘null’ or nil using a while true loop and breaking out of it once it is not ‘null’ or nil, then return the answer. This will make the code inside the chunk of the function run until it is successful, without calling the function multiple times, so that the print(getgame()) fuction doesn’t recieve nothing.

Another approach could be to make make the function return itself if uniId is ‘null’ or nil, by changing the getgame() function, which just runs the function again, with return(getgame()), which will make it return the code ran once again’s return.

I can give you some examples of how these fixes could look if you’d like to.

//gurkleffe

Im not sure why this happens, maybe because of what @gurkleffe said but I know a way to fix it.

Instead of calling tonumber() in the return call it when you are printing.
print(tonumber(getgame()))

else
print(uniId)
return uniId
end

1 Like

Changing

	if uniId == 'null' or nil then
		getgame()

to

	if uniId == 'null' or nil then
		return getgame()

should also make it a bit safer.

1 Like

That worked! I tried it and I got no nil or nulls! Thank you!

1 Like

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