How do I make a script that gives a badge when they reach a certain score of the leader stats, for example, someone hits 60 minutes and he gets a badge.
If you can help, please do.
How do I make a script that gives a badge when they reach a certain score of the leader stats, for example, someone hits 60 minutes and he gets a badge.
If you can help, please do.
Assuming you already have a leaderstat, try this.
while wait(1) do
local player = script.Parent.Parent
player.leaderstats.stat += 1
if player.leaderstats.stat >= 3600 then -- one hour in seconds
BadgeId = 2125029134 --put your badge ID here
local b = game:GetService("BadgeService")
b:AwardBadge(player.userId,BadgeId)
end
end
Try placing this script in StarterPlayerScripts for it to work.
It doesn’t seem to work for some reason.
-- place this in workspace or ServerScriptService
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local time = Instance.new("IntValue")
time.Name = "Points"
time.Parent = leaderstats
Then insert this script into StarterCharacterScripts
repeat
wait()
until
script.Parent.Parent:FindFirstChild("leaderstats")
while wait(1) do
local player = script.Parent.Parent
player.leaderstats.time.Value += 1
if player.leaderstats.time.Value >= 3600 then -- one hour in seconds
BadgeId = 2125029134 --put your badge ID here
local b = game:GetService("BadgeService")
b:AwardBadge(player.userId,BadgeId)
end
end
This is my leaderstat
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Minutes = Instance.new("IntValue")
Minutes.Name = "Minutes"
Minutes.Value = 0
Minutes.Parent = Leaderstats
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Minutes.Value = Data
end
coroutine.resume(coroutine.create(function()
while true do
wait(60)
Minutes.Value = Minutes.Value + 1
end
end))
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
end)
I might use this if everything doesn’t work out, thanks.
I actually made a topic where it has a similar problem: Awards a badge when it reaches a certain stage (Obby)
I tried my own script and changed up the leaderstat name and it worked
Add this script into ServerScriptService and it should work:
Instead of Coins write your leaderstat instead.
local BadgeService = game:GetService("BadgeService")
local BadgeID = --Badge ID
local AmountNeeded = 10
game.Players.PlayerAdded:Connect(function(Player)
Player.leaderstats.Coins.Changed:Connect(function()
if Player.leaderstats.Coins.Value >= AmountNeeded then
BadgeService:AwardBadge(Player.UserId, BadgeID)
end
end)
end)
Try this
Make sure to replace BADGE_ID with your badge ID
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Minutes = Instance.new("IntValue")
Minutes.Name = "Minutes"
Minutes.Value = 0
Minutes.Parent = Leaderstats
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Minutes.Value = Data
end
coroutine.resume(coroutine.create(function()
while true do
wait(60)
Minutes.Value = Minutes.Value + 1
end
end))
Minutes.Changed:Connect(function()
if Minutes.Value >= 60 then
game:GetService("BadgeService"):AwardBadge(Player.UserId, BADGE_ID)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
end)
I provided a valid badge awarding script in the previous thread, for your current script you just need to make the following change.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
local Badges = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BadgeId = 0 --Change to ID of badge.
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Minutes = Instance.new("IntValue")
Minutes.Name = "Minutes"
Minutes.Value = 0
Minutes.Parent = Leaderstats
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Minutes.Value = Data
end
coroutine.resume(coroutine.create(function()
while true do
wait(60)
Minutes.Value = Minutes.Value + 1
if Minutes.Value >= 60 then --1 hour.
local Success, Result = pcall(function()
return Badges:UserHasBadgeAsync(Player.UserId, BadgeId)
end)
if Success then
if not Result then
local Success2, Result2 = pcall(function()
return Badges:AwardBadge(Player.UserId, BadgeId)
end)
if Success2 then
if Result2 then
print(Result2)
end
else
warn(Result2)
end
end
else
warn(Result)
end
end
end
end))
end)
Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
end)
On an additional note, you should wrap your DataStore requests inside calls to the pcall()
global as to handle any potentially thrown/raised errors, i.e; if the DataStore request fails/connection to the DataStoreService drops.
Does this only work when you play 1 hour in-game straight without leaving the server?
Because it doesn’t seem to work
Did you wait for 1 hour or are you changing the value in some other way?
OOOhhh, the minute leaderstat is supposed to save, and slowly get to 1 hour, not play the game for 1 hour in one shot.
How did you test the badge awarding though? Your Minutes
value needs to be greater than or equal to 60.
Yes, my minutes value is like 10 trillion.
So after testing myself, it works, you must be changing the value locally.
Bare in mind, you need to wait for the next minute window for the badge to be awarded.
Can I change the wait(60)
into any number so people won’t need to wait 1 minute?
You only need to wait a minute if you already have a total time greater than 60, otherwise it’ll be awarded as soon as you reach 60.