Help with rewarding badges with just one script

  1. What do you want to achieve? Keep it simple and clear!

I have a working system for this already although my scripting skills are not the best so i have been using multiple separate scripts to award the badges i would like to “unclutter” the ServerScriptService in my game by using one script to award the badges instead of 1 script for each badge.

  1. What is the issue? Include screenshots / videos if possible!

I can not seem to find a way to be able to reward the badges via one script i have searched related topics but to no avail.

Example of the clutter:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have not found any that work for me and the ones on the Developer Hub only handle one badge not multiple.

The code in each individual script to reward the badges:
(in my game i have the BadgeIds and AmountNeeded already in the script so thats not the issue)

local BadgeService = game:GetService("BadgeService")
local BadgeID = 000000
local AmountNeeded = 0000

game.Players.PlayerAdded:Connect(function(Player)
	Player.leaderstats.XP.Changed:Connect(function()
		if Player.leaderstats.XP.Value >= AmountNeeded then
			BadgeService:AwardBadge(Player.UserId, BadgeID)
		end
	end)
end)

Any help would be appreciated
Sorry if i said some things wrong coding is not my main thing

Hello, try something like that

local BadgeService = game:GetService("BadgeService")
local AmountNeededForBadge1 = 0000
local AmountNeededForBadge2 = 1000
local AmountNeededForBadge3 = 2000

game.Players.PlayerAdded:Connect(function(Player)
	Player.leaderstats.XP.Changed:Connect(function()
		local XP = Player.leaderstats.XP.Value
		local badgetogive = 0

		if XP >= AmountNeededForBadge1 and XP < AmountNeededForBadge2  then
			badgetogive = YOUR_BADGE_ID_1

		elseif Player.leaderstats.XP.Value >= AmountNeededForBadge2 and XP < AmountNeededForBadge3 then --- We check if we are in the requirements
			badgetogive = YOUR_BADGE_ID_2

		elseif Player.leaderstats.XP.Value >= AmountNeededForBadge3 then --- Here we don't need to check if it's between two badges
			badgetogive = YOUR_BADGE_ID_3
		end
		BadgeService:AwardBadge(Player.UserId, badgetogive)
	end)
end)

This is not working for me also no errors in the output

Edit: I deleted the badges from my inventory before testing so it not rewarding them because i have them already is not the issue

You could make a framework for handling your badges, and then require it.
I setup a framework to handle your badges in table.

Module Script

local badges = {}
local method = {}
local BadgeService = game:GetService("BadgeService")
method.__index = {}


badges.badges = {
	["TestingBadge"] = {
		name = "TestingBadge",
		value = 2143377204
	},
	["newbadge"] = {
		name = "namehere",
		value = 000000
	}
}



function badges.New(player)
	local self = setmetatable(badges, method)
	
	self.Player = player
	
	return self
end


function badges:Award(whatToAward)
	if self.Player ~= nil then 
		
		local id = self.Player.UserId	
		for index, property in pairs(badges.badges) do 
			if index == whatToAward then 
				BadgeService:AwardBadge(id, property.value)
			end
		end
		
	else 
		warn("Didn't get the player")
	end
end

return badges


Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local badgeAPI = require(ReplicatedStorage:WaitForChild("BadgeApi"))
local badges = badgeAPI.badges

local AmountNeeded = 0000


game.Players.PlayerAdded:Connect(function(Player)
	Player.leaderstats.XP.Changed:Connect(function()
		if Player.leaderstats.XP.Value >= AmountNeeded then
			badgeAPI.New(Player):Award(badges.TestingBadge.name)
		end
	end)
	
end)

This would be more efficient as it doesn’t require you to do local badge = badgeid and then award it in a different script each time, instead you can now just use the badgeapi.New(Player):Award() function instead.

In the future I’d recommend checking out these documentations

These will help you achieve stuff like this later when you are coding as well.

Hope this helped you.

1 Like

Xynterical gave you a modular solution, did he solve your script problem ?

This is not working for me this was in the output

You need to apply the module script code into a modulescript, and require it, and then follow what I did.

I put the ModuleScript inside the Server Script and i get the “Infinite yield”
I name the ModuleScript “BadgeApi” still “infinite yield”
i put the ModuleScript named “BadgeApi” into ReplicatedStorage no errors or yields but it does not give the badges and i deleted the badges from my inventory before testing so it not giving me the badge because i already own it cant be the issue

But being me im probably just being dumb and missing some simple thing

Xynterical gave you a brilliant solution. You should mark it as such, so his work may benefit others who see this post in the future.

1 Like