How to give a badge when a certain leaderstat value is reached?

Hi, i have been trying to give a badge to a player when they reached a leaderstat. but this data store already exists: im scared ill accidentally remove every player’s time.

this is the data store script my game uses:

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Time = Instance.new(“IntValue”)
Time.Name = “Time”
Time.Parent = leaderstats

local playerUserId = “Player”…player.UserId

local data

local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)

if success then
Time.Value = data
end

while wait(60) do ---- Time
player.leaderstats.Time.Value = player.leaderstats.Time.Value + 1
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player”…player.UserId

local data = player.leaderstats.Time.Value

myDataStore:SetAsync(playerUserId, data)

end)
this script adds one person to the player’s time every minute

How do i award a badge to a person when they reach X minutes ?
example: link to badge

i’ve tried to look at other forum posts but couldn’t find anything helpful.

1 Like

:warning:Warning you: You should use the NumberValue object for number values.Int value is not recommended.

Here’s updated version:

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

local BadgeId = 2259404204038562 --Your badge id

local Plr = nil

game.Players.PlayerAdded:Connect(function(player)
Plr = player
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Time = Instance.new(“NumberValue”)
Time.Name = “Time”
Time.Parent = leaderstats

local playerUserId = “Player”…player.UserId

local data

local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)

if success then
Time.Value = data
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player”…player.UserId

local data = player.leaderstats.Time.Value

myDataStore:SetAsync(playerUserId, data)

end)

while task.wait(60) do ---- Time
if not game:GetService("BadgeService"):UserHasBadgeAsync(BadgeId) then
game:GetService("BadgeService"):AwardBadge(Plr.UserId,BadgeId)
end
player.leaderstats.Time.Value += 1
end

would this remove the players data?

you have made the loading system so defenseless.do you want me to update it right now?

No,it wont remove the player’s data.But my opinion i recommended you to use ProfileService.Because Roblox’s Datastore is not protectable.But if Synapse can find a solution for this (i mean anticheat) i dont know.

sure, only if you want to and im not wasting ur time lol.

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

local BadgeId = 2259404204038562 --Your badge id

local Plr = nil

game.Players.PlayerAdded:Connect(function(player)
Plr = player
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Time = Instance.new(“NumberValue”)
Time.Name = “Time”
Time.Parent = leaderstats

local playerUserId = “Player”…player.UserId

local data

local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)

if success then
Time.Value = data
else
local attempts = 5
repeat
data = MyDataStore:GetAsync(PlayerUserId)
Time.Value = data
wait()
attempts -= 1
until data ~= nil
end
end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player”…player.UserId

local data = player.leaderstats.Time.Value

myDataStore:SetAsync(playerUserId, data)

end)

while task.wait(60) do ---- Time
if not game:GetService("BadgeService"):UserHasBadgeAsync(BadgeId) then
game:GetService("BadgeService"):AwardBadge(Plr.UserId,BadgeId)
end
player.leaderstats.Time.Value += 1
end
1 Like