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)
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.
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)