Need help with award badge after player reached 5000 cash

So, I made a system where player will award badge after reached 5000 cash. Problem is that when it reached, it throw errors for me.

Here my error message:

ServerScriptService.Leaderboard:17: attempt to index number with 'UserId'

Here my script code:

local Players = game:GetService("Players")
local badgeservice = game:GetService("BadgeService")
local id = 2125649641

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Money"
	cash.Value = 0
	cash.Parent = leaderstats
	
	cash.Changed:Connect(function(plr)
		if cash.Value > 500 then
			badgeservice:AwardBadge(plr.UserId,id)
			print ("Badge award!")
		end
	end)
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

change plr to player
30char-----------------

I did before. Still same error.

‘plr’ you get is a number value - your cash amount
image

Huh? You tell me is this?

cash.Changed:Connect(function(5000)

Parameters

Name Type Default Description
### property string The name of the property that changed

I don’t understand what you mean…

What’s trying to be said here is the following:

Right now, you’re doing:

	cash.Changed:Connect(function(plr)
        print(plr) --this will print the value of the amount of cash the player has.
-- if i had 600 cash, it would print "600".
		if cash.Value > 500 then
			badgeservice:AwardBadge(plr.UserId,id) -- meaning here, you're saying 600.UserId, which doesn't make sense.
			print("Badge award!")
		end
	end)
end

What @EVALDOL is saying is that you must replace Plr.UserId with player.UserId.

1 Like

So the variable you get is the value of property that was changed.
You cannot index number with ‘UserId’ so change ‘plr’ to ‘player’.

I did before. Still same error.

I already did.

Ah, that’s strange. Are you sure you typed it correctly? Just tested it out and got the following:

1 Like

Can you please send your current code with solutions you have tried based on the input of other users in this thread?

1 Like

It still same like they did for me.

Or maybe I put script on wrong location. My script is in ServerScriptService

Try replacing your current code with this modified version:

local Players = game:GetService("Players")
local badgeservice = game:GetService("BadgeService")
local id = 2125649641

local function leaderboardSetup(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"


	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Money"
	cash.Value = 0
	
	cash:GetPropertyChangedSignal("Value"):Connect(function()
		if cash.Value >= 500 then
			badgeservice:AwardBadge(plr.UserId, id)
			print ("Badge award!")
		end
	end)
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)
1 Like

I tired to put what you said but I got this new error:

Expected ':' not '.' calling member function GetPropertyChangedSignal

woops, it was a typo. I have edited my previous comment to have a fixed version

2 Likes

It worked, now new error.

Workspace.Gold.Script:11: attempt to index nil with 'leaderstats'

It also spammed this message: We already gave out badgeId=2125649641 to userId=523068164

what is the code of the script in Workspace.Gold.Script?

Nvm. It fixed. Idk why that happened but hey, at least it normal from now