Why isn't this working?


local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local badges = {
	{id = 825470753469894, name = "Nuala", texture = "rbxassetid://18940489185"},
	{id = 3993436032447000, name = "Sunflower Nuala", texture = "rbxassetid://18944802036"},
	{id = 2794494538841400, name = "Beach Ball Nuala", texture = "rbxassetid://80222689551657"},
	{id = 2143889494944777, name = "Nualawood", texture = "rbxassetid://18946833131"},
	{id = 4176424032128923, name = "Giant Nuala", texture = "rbxassetid://103876632520199"},
	{id = 3182012389732672, name = "Hydrant Nuala", texture = "rbxassetid://100487757734727"},
	{id = 1101050386441915, name = "Sailor Nuala", texture = "rbxassetid://91109574359405"},
	{id = 473377285836462, name = "Nuala Miner", texture = "rbxassetid://128743420133652"},
	{id = 3762525486484396, name = "Nuala in a Car", texture = "rbxassetid://95427629249900"},
	{id = 340050098656158, name = "Golden Nuala", texture = "rbxassetid://126138662021056"},
	{id = 1063303013099351, name = "Fossilized Nuala", texture = "rbxassetid://79465460318986"},
	{id = 1420164253190837, name = "Squirrel Nuala", texture = "rbxassetid://113482393948846"},
	{id = 4358658733723848, name = "Sunala", texture = "rbxassetid://93600606095858"},
	{id = 3402913698605796, name = "Darkness Nuala", texture = "rbxassetid://112489906966646"},
	{id = 1738741922042695, name = "Soaked Nuala", texture = "rbxassetid://114570479911453"},
	{id = 3008797889549368, name = "Pet Nuala", texture = "rbxassetid://112869458098664"},
	{id = 204729745518514, name = "Sorta Secret Nuala", texture = "rbxassetid://70639536016768"},
	{id = 2203150997366991, name = "CEO Nuala", texture = "rbxassetid://79820069482902"},
	{id = 175513168695701, name = "Inverted Nuala", texture = "rbxassetid://132450627819203"},
	{id = 1713165856656032, name = "Nuala Popsicle", texture = "rbxassetid://77270470518850"},
	{id = 454847409910032, name = "Secret Nuala", texture = "rbxassetid://96152411579165"},
	{id = 454847409910032, name = "Glitchala", texture = "rbxassetid://118536585617465"},
	{id = 4476419490344865, name = "Getting Over Nuala", texture = "rbxassetid://73186683114218"},
	{id = 1148612316112491, name = "Perspective Nuala", texture = "rbxassetid://110993534956541"},
	{id = 4417167326052753, name = "Evil Sunflower Nuala", texture = "rbxassetid://84628998129290"},
	{id = 2043023404572736, name = "Enchanted Nuala", texture = "rbxassetid://93246288392901"},
	{id = 2322113928663430, name = "Nuala Tome", texture = "rbxassetid://105171665819956"},
	{id = 3360273555884087, name = "Nuala of the Highest Lair", texture = "rbxassetid://18983536706"},
	{id = 3400104666010003, name = "WeirdCala", texture = "rbxassetid://127502625460014"},
	{id = 4187874656078620, name = "Very Secret Nuala", texture = "rbxassetid://99422042744585"},
	{id = 4351712341478131, name = "Nuala Sundae", texture = "rbxassetid://120606036215366"},
	{id = 2989237371775314, name = "Error Nuala", texture = "rbxassetid://77765934507270"},
	{id = 3820190767963370, name = "Trolla", texture = "rbxassetid://118260270782448"},
	{id = 1375269726864897, name = "Corrupted Nuala", texture = "rbxassetid://107120486096645"},
	{id = 2407518871994148, name = "Nuala Constellation", texture = "rbxassetid://120362717482625"},
	{id = 4488432287139738, name = "Super Secret Nuala", texture = "rbxassetid://70980098280356"},
	{id = 2488018575890184, name = "Nuala of the Upside Down", texture = "rbxassetid://112448225990430"},
	{id = 947573328526532, name = "Insanely Secret Nuala", texture = "rbxassetid://76411513054539"},
	{id = 2293209590453508, name = "Nualas in Time", texture = "http://www.roblox.com/asset/?id=139010281450474"},
	{id = 2909418844436231, name = "Black Hole Nuala", texture = "rbxassetid://18940489185"},
	{id = 1805495013054783, name = "Difficulty Chart Nuala", texture = "rbxassetid://111085992573816"},
	{id = 3431167427759979, name = "Truly Trolling Nuala", texture = "rbxassetid://133523563740746"},
	-- More badges as needed,
	-- ...
}

local totalBadges = #badges

-- Function to retry badge checks
local function safeUserHasBadgeAsync(userId, badgeId, retryCount)
	retryCount = retryCount or 0
	print("Checking if user " .. userId .. " has badge " .. badgeId)
	local success, result = pcall(function()
		return BadgeService:UserHasBadgeAsync(userId, badgeId)
	end)

	if success then
		print("Badge check succeeded for user " .. userId .. " and badge " .. badgeId)
		return result
	else
		if retryCount < 3 then
			wait(1) -- Wait a bit before retrying
			print("Badge check failed for user " .. userId .. " and badge " .. badgeId .. ". Retrying...")
			return safeUserHasBadgeAsync(userId, badgeId, retryCount + 1)
		else
			print("Failed to check badge " .. badgeId .. " for user " .. userId)
			return false
		end
	end
end

-- Function to update badge position inside the ScrollFrame
local function updateBadgePositionAndSize(dexImage, positionIndex)
	local badgesPerRow = 5
	local badgeSize = UDim2.new(0.1, 0, 0.1, 0)
	local spacing = UDim2.new(0.05, 0, 0.05, 0)

	local baseOffsetX = UDim2.new(.1, 0, 0, 0)
	local baseOffsetY = UDim2.new(0, 0, 0.1, 0)

	local row = math.floor((positionIndex - 1) / badgesPerRow)
	local column = (positionIndex - 1) % badgesPerRow

	if dexImage then
		dexImage.Position = UDim2.new(column * (badgeSize.X.Scale + spacing.X.Scale), 0, row * (badgeSize.Y.Scale + spacing.Y.Scale), 0) + baseOffsetX + baseOffsetY
	else
		warn("dexImage is nil for badge at index: " .. tostring(positionIndex))
	end
end

-- Function to check badges and update ScrollFrame
local function checkBadges(player, scrollFrame, badgeCounterLabel, retries)
	retries = retries or 0
	print("Checking badges...")
	local badgeCount = 0 -- Track the number of badges found

	for index, badge in ipairs(badges) do
		print("Checking badge:", badge.name)
		local hasBadge = safeUserHasBadgeAsync(player.UserId, badge.id, retries)
		local dexImage = scrollFrame:FindFirstChild(badge.name)

		if dexImage then
			print("Found badge image:", badge.name)
			dexImage.Image = badge.texture
			updateBadgePositionAndSize(dexImage, index)

			if hasBadge then
				print("Badge is owned:", badge.name)
				dexImage.ImageTransparency = 0
				local textLabel = dexImage:FindFirstChildOfClass("TextLabel")
				if textLabel then
					textLabel.TextColor3 = Color3.new(0, 1, 0) -- Green for owned badges
				end
				badgeCount = badgeCount + 1 -- Increment the badge count
			else
				print("Badge is not owned:", badge.name)
				dexImage.ImageTransparency = 0.5
				local textLabel = dexImage:FindFirstChildOfClass("TextLabel")
				if textLabel then
					textLabel.TextColor3 = Color3.new(1, 1, 1) -- White for unowned badges
				end
			end
		else
			print("Badge image not found:", badge.name)
		end
	end

	print("Badge count:", badgeCount)
	-- Update the badge counter (X / Y format)
	if badgeCounterLabel then
		badgeCounterLabel.Text = "" .. badgeCount .. " / " .. totalBadges .. " Nualas"
	end
	if badgeCount >= 20 then
		print("20 badges have been found!")
		-- Add any action you want to perform here when 20 badges are found
	end
end

-- Function called when player's character is added
local function onCharacterAdded(character)
	wait(2) -- Slight delay to ensure character is loaded
	print("Character added")
	local player = Players.LocalPlayer
	local playerGui = player:WaitForChild("PlayerGui")
	local screenGui = playerGui:FindFirstChild("ScreenGui")

	if not screenGui then
		warn("ScreenGui not found!")
		return
	end

	local dexFrame = screenGui:FindFirstChild("dexPage1")
	if not dexFrame then
		warn("dexPage1 frame not found!")
		return
	end

	local scrollFrame = dexFrame:FindFirstChild("ScrollFrame")
	if not scrollFrame then
		warn("ScrollFrame not found inside dexPage1!")
		return
	end

	-- Create a badge counter label if not already present
	local badgeCounterLabel = dexFrame.ScrollFrame:FindFirstChild("BadgeCounter")
	if not badgeCounterLabel then
		badgeCounterLabel = Instance.new("TextLabel")
		badgeCounterLabel.Name = "BadgeCounter"
		badgeCounterLabel.Size = UDim2.new(0.4, 0, 0.1, 0) -- Width is 40% of the parent frame, height is 10%
		badgeCounterLabel.Position = UDim2.new(0.25, 0, 0, 0) -- Center horizontally and place it at the top
		badgeCounterLabel.BackgroundTransparency = 1
		badgeCounterLabel.TextScaled = true
		badgeCounterLabel.TextColor3 = Color3.new(1, 1, 1) -- White text
		badgeCounterLabel.TextStrokeTransparency = 0.5 -- Optional: Adds a stroke to make text stand out
		badgeCounterLabel.Parent = dexFrame.ScrollFrame
	end

	-- Check badges and update ScrollFrame
	checkBadges(player, scrollFrame, badgeCounterLabel, 0)
end

-- Connect the Player.CharacterAdded event
local player = Players.LocalPlayer
player.CharacterAdded:Connect(onCharacterAdded)

-- Handle case where the player's character is already loaded
if player.Character then
	onCharacterAdded(player.Character)
end

when the player joins, the GUI works fine, but when they die, it just glitches out on the last few badges in the list. it doesnt check them and takes forever to order them. Does anyone know why this is happening?

This is probably because the ScreenGui has ResetOnSpawn on

(Either you make it refresh when the player resets with CharAdded function or alternatively just do it without the function since if ResetOnSpawn is on, it will just automatically refresh)

This is often caused because of the property that resets the GUI upon respawns. You might toggle it false first and foremost.

I also noticed that you could check multiple badges at once with a single array of the IDs via CheckUserBadgesAsync:

That was so simple lol. thank you!

1 Like

and thank you for the tip! I will try it out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.