Badge script should work, but doesn't?

Doesn’t matter. You always need a pcall , since errors can easily occur in such cases.
Same goes with DataStore.

‘If a user already has a data, not using pcalls is okay’ - no, it’s not. It’s risky.

Tell me if it’s working, if not, I’ll check the code again

The badge still isn’t being awarded… :confused:

1 Like

What method are you attempting to use? Can you show me what your script looks like?

If you’re still using a local script, remember that badges and other user-data can only be changed from the server!! :slightly_frowning_face:

My script :point_up:

char limittttt

local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge

local badgeId = 0 --Change this. 
local groupId = 0 --Change this. 
local groupRank = 0 --Change this. 

players.PlayerAdded:Connect(function(player)
	local success, result = pcall(player.IsInGroup, player, groupId)
	if success then
		if result then
			local success2, result2 = pcall(player.GetRankInGroup, player, groupId)
			if success2 then
				if result2 then
					if result2 >= groupRank then
						local success3, result3 = pcall(userHasBadge, badges, player.UserId, badgeId)
						if success3 then
							if not result3 then
								local success4, result4 = pcall(awardBadge, badges, player.UserId, badgeId)
								if success4 then
									if result4 then
										return
									end
								else
									warn(result4)
								end
							end
						else
							warn(result3)
						end
					end
				else
					warn(result2)
				end
			end
		end
	else
		warn(result)
	end
end)

This seems to be working on my end.

Bare in mind this needs to be a server script, preferably inside the ServerScriptService container.

1 Like

GetRoleInGroup returns a string - the rank itself.

GetRankInGroup returns a number - the number of the rank.

Your script has multiple syntax errors. Make sure that you’re running this code in the server, and NOT using the reference LocalPlayer. This reference can only be used on the client!

Here’s a tweaked version of your script. Run this inside a server script in ServerScriptService or the Workspace:

local GroupID = 4607825
local MinRank = 232
local BadgeId = 2124660106

game.Players.PlayerAdded:connect(function(Player)
    if Player:GetRankInGroup(GroupId) >= MinRank then --// If player is the minimum rank or above they should get the badge
	     BadgeService:AwardBadge(Player.userId, BadgeId) --// Awards badge to player
	     print("Badge Awarded to " .. Player.Name) --// Prints out who got the badge (for debugging purposes)
    end
end)

Hope this helps! :wink:

Didn’t work. :confused:

char limittttt

The print statement got outputted but no badge was awarded?

PlayerAdded is an event that needs to have ) at the end of it

I realised that and fixed it. :neutral_face:

image

It does work though.

Didn’t work for me…

char limittt

Then unfortunately that means no solution will work for you.

This is strange. The solution Forummer gave you should definitely work,

Is your script a normal script or a local script?

Its a normal script inside of ServerScriptService

I tried with an alternate account at the minimum rank and the badge got awarded? :confused:

Change the >= to >, easy fix.

(this will be blurred)

Now the badge doesn’t get awarded at all! It just prints the print statement that I put…