Leaderstat badge giver

Why is this badge giver not working correctly? I have the correct id and everything.

local BadgeID = 2128142670
local Cash = "Wins"

local HowMuch = 3
game.Players.PlayerAdded:connect(function(p) 
repeat wait() until p:findFirstChild("leaderstats") ~= nil 
		p.leaderstats[Cash].Changed:connect(function() 
		if p.leaderstats[Cash].Value <= HowMuch - 1 then return end 
		game:GetService("BadgeService"):AwardBadge(p.userId, BadgeID) 
	end) 
end)
1 Like

Does it get past the repeat? If not, (but it would be good to do this anyway) replace the until p:findFirstChild("leaderstats") ~= nil with until p:findFirstChild("leaderstats")
I can’t remember exactly how Changed works, but I think it would be good to use GetPropertyChangedSignal("Value") instead.

If none of these work, how far does it get before it doesn’t work?

Alright, so:

  • Make sure you have correct BadgeID

Also, I have simplified your script, eventually maybe fixed your problem as well, it worked for me.

local BadgeID = -- Badge Id --
local Cash = "Wins"
local BadgeService = game:GetService("BadgeService")
local HowMuch = 3

game.Players.PlayerAdded:connect(function(player)
	if player:WaitForChild("leaderstats"):FindFirstChild(Cash) then
		player.leaderstats[Cash].Changed:Connect(function(NewPoints)
			if NewPoints >= HowMuch then
			BadgeService:AwardBadge(player.UserId,BadgeID)
			end
		end)
	end
end) 

I think AnotherPenguin solved this, but heres my script, idk if you need it

local leaderstats = player.leaderstats
local Cash = "Wins"
local badgeService = game:GetService("BadgeService")
local badgeId = 0
if Cash.Value >= 5 then
   badgeService:AwardBadge(player.UserId, badgeId)
end

Now that I think of it, I think I butchered it :laughing:

1 Like

I tried using this code but the script wouldn’t process after this line of code:

if player:WaitForChild("leaderstats"):FindFirstChild(Cash) then

Do you have leaderstats inside player?
Do you have Wins value inside leaderstats?

Are you doing it through server-sided script?

Yes, to all of your questions, this is how I have it set up as well.

image

ok that is very cooooooooooooool

This is what I put inside one of the scripts as well and it is only printing out 2 in the output.

local BadgeID = 2128142670
local Wins = "Wins"
local BadgeService = game:GetService("BadgeService")
local HowMuch = 3

game.Players.PlayerAdded:connect(function(player)
	print("2")
	if player:WaitForChild("leaderstats"):FindFirstChild(Wins) then
		print("3")
		player.leaderstats[Wins].Changed:Connect(function(NewPoints)
			print("4")
			if NewPoints >= HowMuch then
				print("5")
				BadgeService:AwardBadge(player.UserId,BadgeID)
				print("6")
			end
		end)
	end
end)

Update: The badge is given only sometimes in studio but never in the actual game.

1 Like

because u already have the badge even if ur not in the game but by working in studio u can get it