Attempt to index number with UserId

I am making a Welcome Badge in Roblox, and I just encountered the most simple error, yet so difficult to solve. I made a module script called “Badge Handler”, and it contains a shared function that will basically accept a BadgeID and a Player. I tried to use as less lines as possible, and here is how it turned out:

local BadgeHandler = {}

local BadgeService = game:GetService("BadgeService")

function BadgeHandler.Award(BadgeId: NumberValue, Player: Player)
	BadgeService:AwardBadge(Player.UserId, BadgeId)	
end

return BadgeHandler

Pretty simple right?

On the other end, a server script in ServerScriptService is calling the function with this text of code:

local BadgeHandler = require(script.Parent.Parent["Badge Handler"])

game.Players.PlayerAdded:Connect(function(Player: Player)
	BadgeHandler:Award(2390214597728677, Player)
end)

Also very simple.

What could possibly go wrong? Well, for some reason, the module script does not accept the UserID of the player, with the following error:

19:42:13.014 ServerScriptService.Badges.Badge Handler:6: attempt to index number with ‘UserId’ - Server - Badge Handler:6

I can’t wrap my head around it. How could I mess up so badly, that an error would pop on my output in the simplest text of code?

I am very confused and I didn’t want to come up with posting on the DevForum, but here I am.

You’re calling Award using the : operator. This will pass BadgeHandler as the first argument, 2390214597728677 as the second, and Player as the third.

See this post for more information: What is self and how can I use it? - #2 by WallsAreForClimbing

3 Likes

So what do you suggest me to do?

You can either define your method with the : operator or call it with the . operator.

-- Option 1:
function BadgeHandler:Award(BadgeId: NumberValue, Player: Player)
	BadgeService:AwardBadge(Player.UserId, BadgeId)	
end
-- Option 2:
BadgeHandler.Award(2390214597728677, Player)
1 Like

Thanks a lot! It worked flawlessly!

I would also like to mention that you’re using an incorrect type here:

The type of 2390214597728677 is number, not NumberValue. NumberValues are a type of Instance:

You can find more information on how value typing works here:

(This isn’t what was causing your problem though, I just wanted to inform you about it :slight_smile::+1:)

2 Likes