How do I detect if all badges are acquired?

I have this system where, if you have a badge, it updates the trophy number.

So, I was trying to check if you had all badges based off the trophy number.
But, it’s not working.

Here is the script;

local badgeservice = game:GetService("BadgeService")
local serverstorage = game:GetService("ServerStorage")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events

local trophynumber = 0


game.Players.PlayerAdded:Connect(function(plr)
	if badgeservice:UserHasBadgeAsync(plr.UserId, 3200481589664286) then
		print("player has 'Muffin Ending'")
		trophynumber +=1

		local trophy = serverstorage.Trophies:FindFirstChild("Ending1"):Clone()
		trophy.Parent = workspace.Trophies

		print(trophynumber)
		
	end
end)


game.Players.PlayerAdded:Connect(function(plr)
	if badgeservice:UserHasBadgeAsync(plr.UserId, 1653929533276385) then
		print("player has 'Coffee Ending'")
		trophynumber +=1

		local trophy = serverstorage.Trophies:FindFirstChild("Ending2"):Clone()
		trophy.Parent = workspace.Trophies

		print(trophynumber)

	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	if badgeservice:UserHasBadgeAsync(plr.UserId, 813563384476380) then
		print("player has 'Donut Ending'")
		trophynumber +=1

		local trophy = serverstorage.Trophies:FindFirstChild("Ending3"):Clone()
		trophy.Parent = workspace.Trophies

		print(trophynumber)

	end
end)

So, for example, it will print 3 in the output because the player has those 3 endings, and the trophy number will equal 3.

Here are both the systems I tried, which did not work.

(In the same script)

if trophynumber == 3 then
	game.Workspace.Cafe.Outside.Door.Glass.DoorScript.Enabled = true
end

OR

if #serverstorage.Trophies:GetChildren() == 3 then
	game.Workspace.Cafe.Outside.Door.Glass.DoorScript.Enabled = true
end

How come it doesn’t work?

If you need any more information, let me know

1 Like

Ok first lets fix this script because its giving me a migrane!

--[[
 While this code may work as is, 
due to speed and focus on clarity, 
there may be a few hitches in here,
 but since I spent the time getting 
it nice and pretty, if it does have 
an issue consider it my effort tax =P
]]
local badgeservice = game:GetService("BadgeService")
local serverstorage = game:GetService("ServerStorage")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events

local trophynumber = 0

local BadgeEnum = {
	Ending1 = "Ending1", 
	Ending2 = "Ending2",
	Ending3 = "Ending3"
}

local BadgeDictionary = {
	Ending1 = 3200481589664286,
	Ending2 = 1653929533276385,
	Ending3 = 813563384476380
}

local BadgePhrases = {
	Ending1 = "player has 'Muffin Ending'",
	Ending2 = "player has 'Coffee Ending'", 
	Ending3 = "player has 'Donut Ending'"
}


--[[
TODO
]]
local function _hasBadge(PlayerInstance, BadgeName)
	return badgeservice:UserHasBadgeAsync(PlayerInstance.UserId, BadgeDictionary[BadgeName])
end

--[[
TODO
]]
local function _grantTrophy(BadgeName)
	
	print(BadgePhrases[BadgeName])
	
	local trophy = serverstorage.Trophies:FindFirstChild(BadgeName):Clone()
	trophy.Parent = workspace.Trophies
end

--[[
Why are you hooking this up to to a bunch of different player added ? 
This is going to cause you all sorts of problems as a whole dont do this 
if possible, try to contain all your logic that you need to run on add into 1 Hook up
--]]

-- Stop Abreviating varables like "plr" its bad bad bad and its super bad habits 
-- kick it now so you don't turn into a talented trash coder
game.Players.PlayerAdded:Connect(function(player)
	
	-- OK so there are lots of fancy ways to do this, and I would do this differently 
	-- BUT let me help you with some logic, due to how we want to check this, we will actually check it 
	-- in reverse because they cant get 3 before 1 and 2 before 3, so 3, 2, 1.  
	
	if _hasBadge(player, "Ending3") then
		
		_grantTrophy("Ending3")
		trophynumber = 3
		
	elseif _hasBadge(player, "Ending2") then 
		
		_grantTrophy("Ending2")
		trophynumber = 2
		
	elseif _hasBadge(player, "Ending1") then
		
		_grantTrophy("Ending1")
		trophynumber = 1	
			
	end
	
	--[[
	Its unclear how you want to use trophy number but because I can safely assume you have one trophy you had to 
	have the others, then instead of adding it up we can just set it because we can assume if you have 3 you have 
	the other two.
	
	If this is not the case then, you can break this out of an elseIf chain into If checks and Add as they cert
	
	Now isnt that nice and so much easier to read and understand wahts going on?
	Don't answer that question I already know it is =P GO FORTH, be better SEEEK the enlightend path of 
	readable code my Brother/Sister/insert approperate proune!  Go forth and run from thy tempation of 
	evil lazy ways!!!!
	( I know dramatic but hey if im gonna basically half code this for you im gonna have fun with it)
	]]
	
end)

Play with this see if your stuff fixes, I have a feeling it has to do with the multiple hook ups to PlayerAdded, but really you shouldn’t need multiple hook ups to it. That event only throws once, so in reality even if you wanted to take a different structural approach. Break your checks out into functions and then insert all the check functions into that event instead of hooking into it a bunch of times.

1 Like

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