Badge will not award because "invalid argument #2 (string expected, got Instance)"

When I try awarding a badge to a player via a remote event, I keep getting an error for the line BadgeService:AwardBadge(PS[playerName].UserId, badgeId)

The error looks like this:

ServerScriptService.remoteEvents.Award42Serverside:9: invalid argument #2 (string expected, got Instance) - Server - Award42Serverside:9

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:FindFirstChild("Award42")

local function award42(playerId)
	local badgeId = 2150026267
	local BadgeService = game:GetService("BadgeService")
	local playerName = playerId
	local PS = game:GetService("Players")
	BadgeService:AwardBadge(PS[playerName].UserId, badgeId)
	print("Awarded Badge")
end

remoteEvent.OnServerEvent:Connect(award42)

I’m pretty sure that Argument #2 means the variable badgeId, and I’m not sure why it’s expecting a string:

image

What am I doing wrong?

1 Like

playerName is an Instance, it’s erroring in this section: PS[playerName].UserId
Player is passed to Remotes first argument by default. You need to set playerName to playerId.Name

Or, simplify the code, take BadgeService out of the function (it’s redundant to call GetService multiple times, it’s not changing) as well as no need for PlayerService, just use playerId.UserId

Thank you! That worked for me.

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