Hello. I’m making a system that awards the player after reaching a certain number of a value inside of leaderstats. Issue is that it’s throwing up an error that says that leaderstats isn’t a child of player. Should i just add a :waitforchild? What’s the issue?
local badgeService = game:GetService("BadgeService")
game.Players.PlayerAdded:Connect(function(player)
if player:WaitForChild("leaderstats") then
player.leaderstats.Bricks.Changed:Connect(function()
local function awardBadge(player, badgeId)
if player.leaderstats.Bricks.Value >= 1 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272327) then
badgeService:AwardBadge(player.UserId, 2149272327)
end
if player.leaderstats.Bricks.Value >= 100 then
if not badgeService:UserHasBadgeAsync(player.UserId,2149272333) then
badgeService:AwardBadge(player.UserId, 2149272333)
end
end
end
if player.leaderstats.Bricks.Value >= 1000 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272344) then
badgeService:AwardBadge(player.UserId, 2149272344)
end
end
if player.leaderstats.Bricks.Value >= 10000 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272355) then
badgeService:AwardBadge(player.UserId, 2149272355)
end
end
if player.leaderstats.Bricks.Value >= 100000 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272400) then
badgeService:AwardBadge(player.UserId, 2149272400)
end
end
end
end)
end
end)
Yes. Inside of my datastore script. It gets created whenever the player joins. But that shouldn’t be an issue if I just re-run the script right? The current script doesn’t even run at all… but without the :waitforchild line it throws up the leaderstat error.
Shouldn’t matter as long as your making the leaderstats folder, (as the error is from the leaderstats folder). Just be aware you don’t get the badges if they aren’t owned by the same universe, and those lines, will throw errors
Also you have a local function but your not calling it at all. Just remove it, like this
local badgeService = game:GetService("BadgeService")
game.Players.PlayerAdded:Connect(function(player)
repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil
player.leaderstats.Bricks.Changed:Connect(function()
if player.leaderstats.Bricks.Value >= 1 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272327) then
badgeService:AwardBadge(player.UserId, 2149272327)
end
if player.leaderstats.Bricks.Value >= 100 then
if not badgeService:UserHasBadgeAsync(player.UserId,2149272333) then
badgeService:AwardBadge(player.UserId, 2149272333)
end
end
end
if player.leaderstats.Bricks.Value >= 1000 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272344) then
badgeService:AwardBadge(player.UserId, 2149272344)
end
end
if player.leaderstats.Bricks.Value >= 10000 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272355) then
badgeService:AwardBadge(player.UserId, 2149272355)
end
end
if player.leaderstats.Bricks.Value >= 100000 then
if not badgeService:UserHasBadgeAsync(player.UserId, 2149272400) then
badgeService:AwardBadge(player.UserId, 2149272400)
end
end
end
end
end)
And because I’m bored I made a nicer verision of this script thats easier to read like this :
local badgeService = game:GetService("BadgeService")
local EachMileStone = {
[1] = 2149272327,
[100] = 2149272333,
[1000] = 2149272344,
[10000] = 2149272355,
[100000] = 2149272400,
}
game.Players.PlayerAdded:Connect(function(player)
repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil
player.leaderstats.Bricks.Changed:Connect(function()
for i, v in EachMileStone do
if player.leaderstats.Bricks.Value >= i then
if not badgeService:UserHasBadgeAsync(player.UserId, v) then
badgeService:AwardBadge(player.UserId, v)
end
end
end
end)
end)
Let me know if this works! Edit: Fixed a Missing end )
Awesome! Thanks for your help. One note though. I added print functions to see what badge would be given and it just prints every badge that the player can get every time the value changes. Which is technically what I want but is there a way for me to stop that or is it gonna be just fine with the BadgeService and it is just the print statements?
local badgeService = game:GetService("BadgeService")
local EachMileStone = {
[1] = 2149272327,
[100] = 2149272333,
[1000] = 2149272344,
[10000] = 2149272355,
[100000] = 2149272400,
}
local DoTheyHaveThisBadgeAlready = {}
game.Players.PlayerAdded:Connect(function(player)
repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil
player.leaderstats.Bricks.Changed:Connect(function()
for i, v in ipairs(EachMileStone) do
if player.leaderstats.Bricks.Value >= i and table.find(DoTheyHaveThisBadgeAlready, v) == nil then
if not badgeService:UserHasBadgeAsync(player.UserId, v) then
badgeService:AwardBadge(player.UserId, v)
end
table.insert(DoTheyHaveThisBadgeAlready, v)
elseif player.leaderstats.Bricks.Value < i then
return
end
end
end)
end)