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:
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)
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.