I’m looking to give a badge to a player when they get 50 kills in a single game (without leaving) how can I script this?
Wherever you have the code to increment the Kills
stat of the player, check if that Value is above 50. If it, then use BadgeService’s AwardBadge() function to grant the player the badge.
How can I script this though? Sorry for the inconvenience, I’m super brand new to scripting so I need alittle help. How does the script award the player when reaching 50 kills?
Depending on how you increment your kills stat, it would be something like this:
-- Services
local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
-- Variables
local BadgeId = 0 -- Put the badgeId here
local function OnPlayerAdded(Player)
local success, HasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(Player.UserId, badgeID)
end)
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Value = 0
Kills.Parent = leaderstatsFolder -- assuming you have one
Kills.Changed:Connect(function(NewValue)
if NewValue >= 50 and (not HasBadge) then
-- Player has 50+ kills AND doesn't have the badge, give it to them
BadgeService:AwardBadge(Player.UserId,badgeId)
end
end)
end
-- Events
Players.PlayerAdded:Connect(OnPlayerAdded)
Thank you! Sorry about the extra clarification, just new to scripting.
Do you have a leaderstats script yet? If you do then share it and I’ll add in support for badge awarding for you.
Ohh that’s why when I tested this out and it didn’t work. I don’t have that. I’ll look up a tutorial on YouTube and get back to you when I got the script.
(I started yesterday on scripting so I kinda need help on this sort of stuff, sorry)
local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")
local badge50Kills = 0 --change to badge id
players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Value = 0
kills.Parent = leaderstats
local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Value = 0
deaths.Parent = leaderstats
kills.Changed:Connect(function(newVal)
if newVal >= 50 then
if badgeService:UserHasBadgeAsync(player.UserId, badge50Kills) then
print("User has badge already!")
else
badgeService:AwardBadge(player.UserId, badge50Kills)
print("Badge awarded!")
end
end
end)
end)
This is a leaderstats and badge awarder all in one, just copy and paste the code into a server script and place it inside the “ServerScriptService” folder.
Like so:
Easy to do, I’m not at home so I can’t show much but here’s a basic leaderstat script
game.Players.PlayerAdded:Connect:function(plr)
local leaderstats = instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = plr
local Kills = instance.new(“IntValue”)
Kills.Name = “Kills”
Kills.Parent = leaderstats
end)
this is very minimal, and as I said I am not home so I couldn’t make it much detailed, but this is a basic idea. If you need extra help I’ll be glad to help
I get the jist of it, thank you! Totally fine with it being minimal, it gives you the point of leaderstats. Thanks!
Ok so, I have already had leaderstats. Knockouts and Wipeouts. How can I award the badge with this leaderstat?
If you provide the script I can add it in.
There seems to be an error. Leaderstats doesn’t update. (Ex. If I kill player, leaderboard says Kills: 0) Please help.
Sorry for the bump, but the script also does not work. (I’m using a gear)