How do I fix Argument 1 missing or nil

Hello, I wanna make a donation leaderboard but I get this error al the time. Its only this error I have tried fixing it, but nothing works… can somebody help me?

for rank, players in ipairs(topTwenty) do
		local userId = players.key
		local amount = players.value
		local player = game.Players:GetPlayerByUserId(userId) -- this is where the error happens
end

its just a part of the script if you need more tell me!
any help is appreciated!

1 Like

Copy the actual error line from your output window so we can see which line it’s looking at.

Try printing each variable before you use it to troubleshoot what’s going on. Something like:

print("rank = ", rank, "  players = ", players) 
for rank, players in ipairs(topTwenty) do
		local userId = players.key
		local amount = players.value
		local player = game.Players:GetPlayerByUserId(userId)
end

If that tells you nothing you wouldn’t expect then print other variables before you use them.

its getplayerbyuserid thats the line

So print topTwenty to see what the value is. You may be trying to read more players than are actually there.
I’m thinking you should be reading amount before the rank, players loop then using amount instead of topTwenty.

I printed the toptwent:

{
                    [1] =  ▼  {
                       ["key"] = "1052422574",
                       ["value"] = 2000
                    },
                    [2] =  ▼  {
                       ["key"] = "Instance",
                       ["value"] = 10
                    }
                 }

thats what came out of the output

how do i get the instance out of there

This possibly occured due to another error in another script. You saved an Instance as a key, probably that you saved the Player instead of the Player.UserId.

Check in all your scripts for “:SetAsync(” (using find all) and make sure you’re saving correctly.

1 Like

I just got a message on Roblox from someone who reads the posts here but can’t reply. I’ve copy/pasted the entire message here. If it works for you please message them to thank them:

Diduiem@DiduiemMay 15, 2024 | 11:50 AM

Hey there!

I saw you recently replied to a topic titled “How do I fix Argument 1 missing or nil”, where a youngster was trying to figure out what was wrong with his code. Essentially, I figured out what’s wrong, but I’m not a Member on the devforum yet, so I’ve been confined to watching. Not sure if you’ll receive this message, but I hope I can at least get you a free solution for it.

The issue is that he’s trying to read the ‘key’ property of userId, but he assigned that value as a string. So, what he has to do is instead of using userId.key, he should use userId[“key”] to retrieve the User ID.

Additionally, the GetPlayerByUserId() requires an integer. And since he stored the User ID as a string, we’ll have to first convert it to an integer using tonumber(userId[“key”]).

If he updates his code to instead read local player = game.Players:GetPlayerByUserId(tostring(userId[“key”]))

Then the error should be resolved.

I hope you get this message before someone else snags it! Have a nice day.

1 Like

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