[HELP] variable access

Hello, i don’t seem to be able to access the variable from within “function(user)”

function getPlayerRank(identifier)
	rank = ""
	db.getUser(identifier, function(user)
		rank = user["rank"]
		print("rank1:"..rank)
	end)
	print("rank2:"..rank)
	return rank
end

The output is:

Try local rank = ""
for that first line

still the same problem.

Both are working. You set rank to “” and rank2 prints empty? What exactly do you expect?

So rank is set to blank. Rank is set in the function and displays NONE. And rank2 displays as empty which is correct. Notice how rank2 prints first? That’s because it runs before the function code.

I think the function(user) part only works in a Citizen.CreateThread or eventhandler.
If you want to use the user in a function, you have to pass it as a parameter. (I can be totally wrong on this one off-course)

No, it all works as intended. When you create that function part, it doesn’t run right away. It runs at the end of the block.

The code runs in this order

rank = “”
print(“rank2:”…rank)
db.getUser(identifier, function(user)
rank = user[“rank”]
print(“rank1:”…rank)
end)

Which is why the output is
rank2:
rank1: NONE

The rank variable is getting changed, but it’s AFTER the rank2 output.