local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeID = 2124809547 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
local chr = script.Parent
local part = game.ReplicatedStorage.GucciWatch:Clone()
part.Parent = chr
local weld = Instance.new("Weld",chr)
weld.Part1 = part.Union
weld.Part0 = chr:WaitForChild("Left Arm")
weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
else
local chr = script.Parent
local part = game.ReplicatedStorage.Watch:Clone()
part.Parent = chr
local weld = Instance.new("Weld",chr)
weld.Part1 = part.Union
weld.Part0 = chr:WaitForChild("Left Arm")
weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awarded, errorMessage = pcall(function()
BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awarded then
warn("Error while awarding badge:", errorMessage)
end
end
else
warn("Error while fetching badge info!")
end
end
game:GetService("ReplicatedStorage").Gucci.OnServerEvent:Connect(function(player)
awardBadge(player,2124809547)
end)
Players.PlayerAdded:Connect(onPlayerAdded)
So i have a script that gives players a watch that they can stare at, Your first watch is broken but if you stare at it for 10 minutes, you get a slightly less dinky one and a badge to say that you have it.
For some reason when I try to use this script to decide which to give them, it just doesen’t give them any of them, anyone know why?
PCalls return two values, a boolean that is true if there wasn’t an error, and an error message if it did. If it didn’t error, that error message will be nil. You should instead change this to;
local hasBadge;
local success, errorMessage = pcall(function()
hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID);
end)
I tried changing how it detects which that i have, but it still gives me the default watch
local bruh = false
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeID = 2124809547 -- Change this to your badge ID
local function onPlayerAdded(player)
local hasBadge;
-- Check if the player has the badge
local success, errorMessage = pcall(function()
hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID);
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
bruh = true
local chr = script.Parent
local part = game.ReplicatedStorage.GucciWatch:Clone()
part.Parent = chr
local weld = Instance.new("Weld",chr)
weld.Part1 = part.Union
weld.Part0 = chr:WaitForChild("Left Arm")
weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
if bruh == false then
local chr = script.Parent
local part = game.ReplicatedStorage.Watch:Clone()
part.Parent = chr
local weld = Instance.new("Weld",chr)
weld.Part1 = part.Union
weld.Part0 = chr:WaitForChild("Left Arm")
weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
end
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awarded, errorMessage = pcall(function()
BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awarded then
warn("Error while awarding badge:", errorMessage)
end
end
else
warn("Error while fetching badge info!")
end
end
game:GetService("ReplicatedStorage").Gucci.OnServerEvent:Connect(function(player)
awardBadge(player,2124809547)
end)
Players.PlayerAdded:Connect(onPlayerAdded)