Invalid argument #2 to 'format' (string expected, got nil)

Greetings,

I have an issue with my code. Please help! The error and code can be found below:

Error:

PlayerDataHandler script lines 87 - 91:

local function getProfile(player)
	assert(Profiles[player], string.format("Profile does not exist for %s", player.UserId)) --Error happens here!
	
	return Profiles[player]
end

PurchaseHandler script lines 16 - 23:

local player = OwnerValue.Value
if game.Players:FindFirstChild(player) then
	if player ~= nil then
		local tycoon = script.Parent.Name
		print("The tycoon name: " ..tycoon)
		PlayerDataHandler.loadTycoonData(player, tycoon)	
	end
end

Line that does the getProfile:

local profile = getProfile(player)

Thanks!

2 Likes

I think special characters (at this case the percent) can cause different havoc, because they could have a purpose for other functions, like the " and ’ symbols.

1 Like

Is there a way to replace that somehow? It would be useful to keep it in case anything happens

You could use a different character, that’s what I got because I’m no smart scripter. And how useful would it be?

Convert the UserID to a string

I solved it finally! I made a newbie mistake by not actually setting the player and only going by that value. Basically I had a string value that showed who owns that tycoon and instead of setting the player from the players, i just didnt at all, so it couldnt find the userid.

1 Like

Change %s to %i or %d
assert(Profiles[player], string.format("Profile does not exist for %i", player.UserId))

when you create this variable

local player = OwnerValue.Value

you are referring to the value of OwnerValue at THE TIME YOU CREATE the variable. You could fix it by having it like this

if game.Players:FindFirstChild(OwnerValue.Value) then
    local player = OwnerValue.Value

	if player ~= nil then
		local tycoon = script.Parent.Name
		print("The tycoon name: " ..tycoon)
		PlayerDataHandler.loadTycoonData(player, tycoon)	
	end

end

and then wrap the getProfile function into a conditional statement of the sort

if game.Players:FindFirstChild(OwnerValue.Value) then
    local profile = getProfile(OwnerValue.Value)
end

EDIT: oh you already fixed this whoops