Cash script that gives user a badge after X amount of cash

Hi there,

Im trying to figure out how to award a badge to a player after they earn X amount of cash via a leaderstat value, there is multiple amounts with multiple badges

local badgeInfo = {
	{id = 2142678232, cashThreshold = 100000},
	{id = 2142678258, cashThreshold = 500000},
	{id = 2142678273, cashThreshold = 1000000},
	{id = 2142678293, cashThreshold = 25000000}
}

function checkForBadge(player, badge_id, cash_threshold)
	if player.leaderstats.Cash.Value >= cash_threshold then
		if not game:GetService("BadgeService"):UserHasBadgeAsync(player.UserId, badge_id) then
			player:FindFirstChildOfClass("BadgeService"):AwardBadge(player.UserId, badge_id)
			print(player.Name .. " has earned the badge!")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	for _, badgeInfo in ipairs(badgeInfo) do
		player.leaderstats.Cash.Changed:Connect(function()
			checkForBadge(player, badgeInfo.id, badgeInfo.cashThreshold)
		end)
	end
end)


* i am not a scripter, this is chat gpt.

if someone could also rewrite the code to make it work it would be appreciated

so the script didn’t work? is there any error?

sorry if i don’t understand what are you trying to achieve here

I see what you’re going for. But I do not suggest using an AI to write your code for you. You should rather make it so that it indicates how much cash the player has so it then gives the badge to the player. It should be a lot less complicated that way.

You’d be much better off just looking at the examples in BadgeService documentation. They literally give you the AwardBadge function. You just need to call it when you reach your thresholds.