Badge for rebirthing error

I’m trying to make some badges for players that get 1,5,20 rebirths, I’ve made a code but It’s not giving the badge to the player. Also is not giving any errors.

badge1rebirth = 2124849496
badge5rebirth = 2124849498
badge20rebirth = 2124849500

wait(10)
game.Players.PlayerAdded:Connect(function(player)
	while true do
	wait(1)
		if player.leaderstats.Rebirths.Value >= 1 then
			game:GetService("BadgeService"):AwardBadge(player.userId,badge1rebirth)
		elseif player.leaderstats.Rebirths.Value >= 5 then
			game:GetService("BadgeService"):AwardBadge(player.userId,badge5rebirth)
		elseif player.leaderstats.Rebirths.Value >= 20 then
			game:GetService("BadgeService"):AwardBadge(player.userId,badge20rebirth)
		end
	end
end)

I think it’s bcs rebirths are probably stored in serverstorage but idk how to access it. I also have a badge for people who are in group to get cash and it works and looks like this that’s why I think rebirths are in serverstorage too:

local group = 12524604
local amount = 10000
local serverStorage = game:GetService("ServerStorage")
local playerMoney = serverStorage:WaitForChild("PlayerMoney")


game.Players.PlayerAdded:Connect(function(player)
	wait(8)
	if player:IsInGroup(group) then
		local plrCash = playerMoney:FindFirstChild(player.Name)
		if plrCash then
			plrCash.Value = plrCash.Value + amount
		end
	end
end)

Not sure if you know this already but badges can only be awarded from a server script.

Also you should probably replace while true do with while task.wait() do, since while true do can often cause scripts to crash.

Wait, what does that actually mean. I’m using a normal script, not a local one and the script is placed in ServerScriptStorage

I was just making sure that you had this placed in a server script & not a local script. Are your leaderstats working correctly?

Yes, pretty sure they are. I’m also using them for multipliers and stuff, and they work good.

local badge1rebirth = 2124849496
local badge5rebirth = 2124849498
local badge20rebirth = 2124849500
local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")

players.PlayerAdded:Connect(function(player)
	local playerStats = player:WaitForChild("leaderstats")
	local rebirthStat = playerStats:WaitForChild("Rebirths")
	while task.wait(1) do
		if rebirthStat.Value >= 1 then
			badgeService:AwardBadge(player.UserId, badge1rebirth)
		elseif rebirthStat.Value >= 5 then
			badgeService:AwardBadge(player.UserId, badge5rebirth)
		elseif rebirthStat.Value >= 20 then
			badgeService:AwardBadge(player.UserId, badge20rebirth)
		end
	end
end)
1 Like

Rebirths do belong to the leaderstats folder right?

Yes it does, and thank you so much, it’s working now!

I’m having a problem thou. For the people who have 20+ rebirth already, they only get the first badge not the rest. Any idea why?

EDIT: It’s actually not working for 5 and 20 rebirths, only the 1+ works

I want to note that you can achieve the same thing much more efficiently using events, listen to the value changes with GetPropertyChangedSignal

1 Like

Change the elseif statements to if statements. it stops looking after seeing the first if is true

1 Like

Or swap them (from checking highest level to lowest instead)

1 Like

Alright, it works, thank you guys!

This feedback should be provided to the original poster, I was merely fixing the error which was occurring.

Or you could simply re-order the conditional statements in reverse order.