Hello, i’ve been trying to add a badge system into my leaderstat script so I people can get badges at certain levels (ex: when you’re level 10, you get a badge). I attempted to do it but for some reason it does not work, could anyone help?
(Badge script is at the end)
--LEADERSTATS AND DATASTORE
local badgeService = game:GetService("BadgeService")
local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore")
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local levels = Instance.new("IntValue")
levels.Name = "Levels"
levels.Parent = leaderstats
local exp = Instance.new("IntValue")
exp.Name = "Exp"
exp.Parent = leaderstats
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
local obbies = Instance.new("IntValue")
obbies.Name = "ObbiesCompleted"
obbies.Parent = leaderstats
local bobMembership = Instance.new("BoolValue")
bobMembership.Name = "bobMembership"
bobMembership.Parent = player
local key = "user-" .. player.userId
local storeditems = datastore:GetAsync(key)
if storeditems then
levels.Value = storeditems[1]
exp.Value = storeditems[2]
points.Value = storeditems[3]
bobMembership.Value = storeditems[4]
obbies.Value = storeditems[5]
else
local items = {levels.Value, exp.Value, points.Value, bobMembership.Value, obbies.Value}
datastore:SetAsync(key, items)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.Levels.Value, player.leaderstats.Exp.Value, player.leaderstats.Points.Value, player.bobMembership.Value, player.leaderstats.ObbiesCompleted.Value}
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
end)
--LEVEL SYSTEM
game.Players.PlayerAdded:Connect(function(plr)
wait(.1)
local exp = plr.leaderstats.Exp
local levels = plr.leaderstats.Levels
exp:GetPropertyChangedSignal("Value"):Connect(function()
if exp.Value >= (100 * (levels.Value + 1)) then
levels.Value = levels.Value + 1
exp.Value = 0
game.ReplicatedStorage.LevelUpGui:FireClient(plr)
end
end)
end)
--BADGE SYSTEM
local levels = game.Players.leaderstats.Levels
local player = game.Players
levels.Changed:Connect(function(newVal)
if newVal >= 10 then
if badgeService:UserHasBadgeAsync(player.UserId, 2128348320) then
print("User has badge already!")
else
badgeService:AwardBadge(player.UserId, 2128348320)
print("Badge awarded!")
end
end
end)