So I have this badge script for when a player becomes a certain rank or above they get a badge for it, but it doesn’t work as I had hoped it would.
Help is greatly appreciated!
My Code
local GroupId = 4607825 --// My Group ID
local Player = game:GetService("Players").LocalPlayer
local BadgeService = game:GetService("BadgeService")
local MinRank = 232 --// Minimum rank that should get the badge
local BadgeId = 2124660106 --// My badge ID
if Player:GetRoleInGroup(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
game:GetService(“Players”) returns the player service not the actual player, this would fix this issue
local GroupId = 4607825 --// My Group ID
local Player = game:GetService("Players").LocalPlayer
local BadgeService = game:GetService("BadgeService")
local MinRank = 232 --// Minimum rank that should get the badge
local BadgeId = 2124660106 --// My badge ID
if Player:GetRoleInGroup(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
The service Players is a service that contains all the players in your game, it doesn’t return you a local player.
Try this code :
local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge
local badgeId = 2124660106
local groupId = 4607825
local MinRank = 232
local function onPlayerAdded(player)
local success, result = pcall(userHasBadge, badges, player.UserId, badgeId) --HasAsync,Service,userID,badgeID
if success then
if not result then
local success2, result2 = pcall(awardBadge, badges, player.UserId, badgeId)
if success2 then
if player:GetRoleInGroup(groupId) >= MinRank then
badges:AwardBadge(player.UserId,badgeId)
end
if result2 then
do end
end
else
warn(result2)
end
end
else
warn(result)
end
end
players.PlayerAdded:Connect(onPlayerAdded)
local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge
local badgeId = 2124660106
local groupId = 4607825
local MinRank = 232
game.Players.PlayerAdded:Connect(function(Player)
local success, result = pcall(userHasBadge, badges, Player.UserId, badgeId) --HasAsync,Service,userID,badgeID
if success then
if not result then
if Player:GetRoleInGroup(groupId) >= MinRank then
badges:AwardBadge(Player.UserId,badgeId)
end
end
end
end)
I suggest you use an event to run a function that ultimately awards the badge.
I’m assuming you’re running this code in a server script, (it would not work in a local script anyways) so an easy fix would look something like this:
game.Players.PlayerAdded:Connect(function(Player)
local GroupId = 4607825 --// My Group ID
local BadgeService = game:GetService("BadgeService")
local MinRank = 232 --// Minimum rank that should get the badge
local BadgeId = 2124660106 --// My badge ID
if Player:GetRoleInGroup(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
If you wish to run this code at another point in time other than when the player is joining, make sure the event returns the player’s instance so the script knows who to award the badge to.
if success then
if not result then
if Player:GetRoleInGroup(groupId) >= MinRank then
badges:AwardBadge(Player.UserId,badgeId)
end
end
end
result here means that the player doesn’t have the badge, so I assume this needs to be re-written to :
game.Players.PlayerAdded:Connect(function(Player)
local success, result = pcall(userHasBadge, badges, Player.UserId, badgeId) --HasAsync,Service,userID,badgeID
if result then
if Player:GetRoleInGroup(groupId) >= MinRank then
badges:AwardBadge(Player.UserId,badgeId)
end
end
end)
If a user already owns a badge, attempting to award one will not return an error. Roblox’s servers cannot and will not attempt to award a badge twice. If they already own the badge, nothing will happen.