For loop to Award Badges

I know there is an easy way to do this, I just don’t know how.

local BadgeService = game:GetService(“BadgeService”)

local Badge1 = 2124451879
local Badge2 = 2124451882
local Badge3 = 2124451878
local Badge4 = 2124451888
local Badge5 = 2124451885
local Badge6 = 2124451887
local Badge7 = 2124458334
local Badge8 = 2124451911
local Badge9 = 2124451881
local Badge10 = 2124451912
local Badge11 = 2124451886
local Badge12 = 2124451883
local Badge13 = 2124451913
local Badge14 = 2124451884
local Badge15 = 2124451889
local Badge16 = 2124454956
local Badge17 = 2124454957
local Badge18 = 2124454958
local Badge19 = 2124454959
local Badge20 = 2124454960
local Badge21 = 2124454961
local Badge22 = 2124454962
local Badge23 = 2124454963
local Badge24 = 2124454964
local Badge25 = 2124454965
local Badge26 = 2124454966
local Badge27 = 2124451880
local Badge28 = 2124454967
local Badge29 = 2124454968
local Badge30 = 2124454969
local Badge31 = 2124454970
local Badge32 = 2124454971
local Badge33 = 2124454972
local Badge34 = 2124454973
local Badge35 = 2124454974
local Badge36 = 2124454975
local Badge37 = 2124454976
local Badge38 = 2124454977
local Badge39 = 2124454978
local Badge40 = 2124454979
local Badge41 = 2124454980
local Badge42 = 2124454981
local Badge43 = 2124454982
local Badge44 = 2124454983
local Badge45 = 2124454984
local Badge46 = 2124454985
local Badge47 = 2124454986
local Badge48 = 2124454987
local Badge49 = 2124454988
local Badge50 = 2124454989
local Badge51 = 2124454990
local Badge52 = 2124454991
local Badge53 = 2124454992
local Badge54 = 2124454993
local Badge55 = 2124454994
local Badge56 = 2124454995
local Badge57 = 2124454996
local Badge58 = 2124454997
local Badge59 = 2124454998
local Badge60 = 2124454999
local Badge61 = 2124455000
local Badge62 = 2124455001
local Badge63 = 2124455002
local Badge64 = 2124455003
local avatartracker = 1

avatartracker = player.records.LastAvatar.Value

–I don’t want to do 64 if then statements.

if avatartracker == 1 then
BadgeService:AwardBadge(player.userId, Badge1)
end

if avatartracker == 2 then
BadgeService:AwardBadge(player.userId, Badge2)
end

if avatartracker == 3 then
BadgeService:AwardBadge(player.userId, Badge3)
end

if avatartracker == 4 then
BadgeService:AwardBadge(player.userId, Badge4)
end

–I know it should look something like this:

for AwardBadge = 1,64 do
if AwardBadge == avatartracker then
BadgeService:AwardBadge(player.userId, “Badge…AwardBadge”)
end
end

2 Likes

Just do a table with the IDs and loop through the table, check if they own the badge and if they don’t attempt to reward them with it and keep retrying until it actually works.

1 Like

Is there a way to make the “Badge…AwardBadge” into Badge1,Badge2,Badge3 based on the award badge. I like the table, but want to easily track down any human error on assigning badges vales to specific badge variables.

Actually I don’t think you even need the for loop if you plan on doing an organized table

1 Like

ok, I see what your saying.

I can use this input format for the table and still be able to track any bugs.
a = {[1]=“red”, [2]=“green”, [3]=“blue”,}

If your using an assigned value avatartracker that is set apart from the badge datatable, and if its sequential which it looks like it is. Then use that value as an index to access the table and it will have a lower complexity O(1) than the loop O(n)

1 Like

Yes, that totally makes sense and makes the code a lot simpler.

Thanks

1 Like

First, I want to say thanks to greatgavin and Lua_Basics. Your investment of a few minutes saved me hours of bad coding.

Second, I am posting the solution.
So many times I read posts and they are like an unfinished crime novel. You know the crime, you hear all of the theories, but you never get to see the final solution. Here is the solution.

local BadgeService = game:GetService(“BadgeService”)
local avatartracker

Badges = {[1] = “2124451879”, [2] = “2124451882”, [3] = “2124451878”, [4] = “2124451888”, [5] = “2124451885”, [6] = “2124451887” , [7] = “2124458334”, [8] = “2124451911”, [9] = “2124451881”, [10] = “2124451912”, [11] = “2124451886”, [12] = “2124451883”, [13]= “2124451913”, [14] = “2124451884”, [15] = “2124451889”, [16] = “2124454956”, [17] = “2124454957”, [18] = “2124454958”,[19] = “2124454959”, [20] = “2124454960”, [21] = “2124454961”, [22] = “2124454962”, [23] = “2124454963”, [24] = “2124454964”,[25] = “2124454965”, [26] = “2124454966”, [27] = “2124451880”, [28] = “2124454967”, [29] = “2124454968”, [30] = “2124454969”,[31] = “2124454970”, [32] = “2124454971”, [33] = “2124454972”, [34] = “2124454973”, [35] = “2124454974”, [36] = “2124454975”, [37] =“2124454976”, [38] = “2124454977”, [39] = “2124454978”, [40] = “2124454979”, [41] = “2124454980”, [42] = “2124454981”, [43] =“2124454982”, [44] = “2124454983”, [45] = “2124454984”, [46] = “2124454985”, [47] = “2124454986”, [48] = “2124454987”, [49] =“2124454988”, [50] = “2124454989”, [51] = “2124454990”, [52] = “2124454991”, [53] = “2124454992”, [54] = “2124454993”, [55] =“2124454994”, [56] = “2124454995”, [57] = “2124454996”, [58] = “2124454997”, [59] = “2124454998”, [60] = “2124454999”, [61] =“2124455000”, [62] = “2124455001”, [63] = “2124455002”, [64] = “2124455003”,}

player.records.LastAvatar.Value = avatartracker

if avatartracker > 0 then
BadgeService:AwardBadge(player.userId, Badges[avatartracker])
end
end

2 Likes

Quick note: You do not need to declare the keys if you are using integers in order starting from one. Lua already does it for you.

1 Like

Yes, but it makes it a lot easier for me to track down clerical mistakes, I may have made in assigning Badges. There are 64 badges and correcting a clerical error on my part is a lot easier if I declare the keys.

You can just have one badge id in one line and use the line numbering of the script editor to have them in a clear order.