Hey everyone,
I am trying to create a script that awards a player a badge for resetting their character. I made this script but it doesn't seem to work. What am I doing wrong here, and how do I fix it? Thank you!
game:GetService('Players').PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function(player)
game:GetService("BadgeService"):AwardBadge(character.userId, 123)
end)
end)
end)
Edit: the “123” is just a dummy for the actual badge ID. I haven’t created the badge yet. However, I am checking the console for the message that the badge is given.
Probably CharacterAdded it’s being connected after the Character loads. Instead use a variable to check if the character already exists.
Character doesn’t have a property that returns UserId, but the player has. Also make sure you change the 123 number to the actual badge id.
game:GetService('Players').PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
character:WaitForChild("Humanoid").Died:Connect(function()
game:GetService("BadgeService"):AwardBadge(player.UserId, 123)
end)
end)
game:GetService("Players").PlayerAdded:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait() or nil
local Humanoid = Character:FindFirstChild("Humanoid")
Humanoid.Died:Connect(function()
game:GetService("BadgeService"):AwardBadge(Player.UserId, 0)
print("Awarding Badge to "...Player.Name)
end)
end)
local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local badgeId = 0 --Change to ID of badge.
local playerBadges = {}
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
if not playerBadges[player] then
local success, result = pcall(function()
return badges:AwardBadge(player.UserId, badgeId)
end)
if success then
if result then
print(result)
playerBadges[player] = true
end
else
warn(result)
end
end
end)
end)
local success, result = pcall(function()
return badges:UserHasBadgeAsync(player.UserId, badgeId)
end)
if success then
if result then
print(result)
playerBadges[player] = true
end
else
warn(result)
end
end)
players.PlayerRemoving:Connect(function(player)
if playerBadges[player] then
playerBadges[player] = nil
end
end)
Just wrote up this script which works (tested), you just need to change the badge ID to that of a badge which belongs to your game. It should go without saying but this needs to be a server script (only the server can distribute/award badges).