Rewarding badge at certain leaderstat value won't work

Hello, i’ve been trying to add a badge system into my leaderstat script so I people can get badges at certain levels (ex: when you’re level 10, you get a badge). I attempted to do it but for some reason it does not work, could anyone help?
(Badge script is at the end)

--LEADERSTATS AND DATASTORE
local badgeService = game:GetService("BadgeService")
local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") 
game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local levels = Instance.new("IntValue") 
	levels.Name = "Levels" 
	levels.Parent = leaderstats

	local exp = Instance.new("IntValue") 
	exp.Name = "Exp" 
	exp.Parent = leaderstats

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
	
	local obbies = Instance.new("IntValue") 
	obbies.Name = "ObbiesCompleted" 
	obbies.Parent = leaderstats

	
	local bobMembership = Instance.new("BoolValue")
	bobMembership.Name = "bobMembership"
	bobMembership.Parent = player

	local key = "user-" .. player.userId

	local storeditems = datastore:GetAsync(key)
	if storeditems then
		levels.Value = storeditems[1] 
		exp.Value = storeditems[2]
		points.Value = storeditems[3]
		bobMembership.Value = storeditems[4]
		obbies.Value = storeditems[5]
		
	else
		local items = {levels.Value, exp.Value, points.Value, bobMembership.Value, obbies.Value} 
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Levels.Value, player.leaderstats.Exp.Value, player.leaderstats.Points.Value, player.bobMembership.Value, player.leaderstats.ObbiesCompleted.Value} 
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)

--LEVEL SYSTEM
game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local exp = plr.leaderstats.Exp
	local levels = plr.leaderstats.Levels

	exp:GetPropertyChangedSignal("Value"):Connect(function()
		if exp.Value >= (100 * (levels.Value + 1)) then
			levels.Value = levels.Value + 1
			exp.Value = 0
			game.ReplicatedStorage.LevelUpGui:FireClient(plr)
		end
	end)
end)

--BADGE SYSTEM
local levels = game.Players.leaderstats.Levels
local player = game.Players

levels.Changed:Connect(function(newVal)
	if newVal >= 10 then
		if badgeService:UserHasBadgeAsync(player.UserId, 2128348320) then
			print("User has badge already!")
		else
			badgeService:AwardBadge(player.UserId, 2128348320)
			print("Badge awarded!")
		end
	end
end)

The line quoted above seems to be the issue. You should merge all of the leaderstat listeners into the original PlayerAdded listener in the beginning. It will make this more efficient and clean it up a bit. There is nowhere in your badge system that references any player whatsoever, therefore it needs to be embedded in a PlayerAdded listener.

1 Like

I tried to do this and it still doesn’t work, any suggestions?

game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local levels = game.Players.leaderstats.Levels
	local player = game.Players
	local badgeService = game:GetService("BadgeService")

	levels.Changed:Connect(function(newVal)
		if newVal >= 10 then
			if badgeService:UserHasBadgeAsync(player.UserId, 2128348320) then
				print("User has badge already!")
			else
				badgeService:AwardBadge(player.UserId, 2128348320)
				print("Badge awarded!")
			end
		end
	end)
end)

Fixed it! It just was some bad wording I did that made it still not work but now it’s working perfectly! Thank you!

game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local levels = plr.leaderstats.Levels
	local badgeService = game:GetService("BadgeService")

	levels.Changed:Connect(function(newVal)
		if newVal >= 10 then
			if badgeService:UserHasBadgeAsync(plr.UserId, 2128348320) then
				print("User has badge already!")
			else
				badgeService:AwardBadge(plr.UserId, 2128348320)
				print("Badge awarded!")
			end
		end
	end)
end)
1 Like
game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local levels = plr.leaderstats.Levels
	local badgeService = game:GetService("BadgeService")

	levels.Changed:Connect(function(newVal)
		if newVal >= 10 then
			if badgeService:UserHasBadgeAsync(plr.UserId, 2128348320) then
				print("User has badge already!")
			else
				badgeService:AwardBadge(plr.UserId, 2128348320)
				print("Badge awarded!")
			end
		end
	end)
end)
1 Like

Yep, this is what I needed to do but it took me a while to realize lol, I’m exhausted so makes sense I did mistakes like these. Thank you still!

If you need any more help go check this. I made this post 1 minute after you lol. I had almost the same problem. Give badge on LeaderStats Value - #39 by LxckyDev

1 Like