Roblox Randomizer Game Badge Items

I am currently making a randomizer game, where in ReplicatedStorage, there are 3 folders: melee, ranged, and special. Each contains several items, and a script chooses a random one and give it to the player after every death.

Everything works fine, but I want to add badge items. I’m thinking I would make secretmelee, secretranged, and secretspecial folders filled with these badge items, and a script which detects if the user has a specific badge, but I don’t know where to go from there.

What are some possible ways I could add the special tools into the normal tool randomizer? Would using a localscript which moves the tool from the special file to the normal file work?

I haven’t tried anything yet since I got the idea right now (at school) and I’m not home yet.

Any help would be appreciated!

P.S. This is my first ever devforum post and I can’t wait to get into the developer community! :smiley:

P.P.S I know a VERY basic amount of Lua, so, in advance, sorry if I don’t understand something you’re trying to tell me.

4 Likes

I don’t know if you want to pick a single weapon from the 3 groups together or 1 of each. Anyways I’ll share my idea with an example that you can generalize to make it suit your intention.

This script is suposed to be a server script in the StarterCharacterScripts folder.

local char = script.Parent

local plrs = game:GetService("Players")
local plr = plrs:GetPlayerFromCharacter(char)

local bS = game:GetService("BadgeService")
local owned = bS:UserHasBadgeAsync(plr.UserId, id) -- id is the ID of your badge

local rep = game:GetService("ReplicatedStorage")
local mle = rep:WaitForChild("Melee") -- whatever your melee folder is called
local sMle = rep:WaitForChild("SecretMelee") -- whatever your secret melee folder is called
local n = #mle:GetChildren()
local m = n

if owned then
    m = m + #sMle:GetChildren()
end

local ran = math.random(1, m)
local chosen = nil

if ran <= n then
    chosen = mle:GetChildren()[ran]
else
    chosen = sMle:GetChildren()[ran - n]
end

local nWep = chosen:Clone()
nWep.Parent = plr.Backpack
3 Likes

Also, to make it more pseudo-random, don’t forget to deal with math.randomseed().
(here I’m just talking about the pseudo-randomizer part)

2 Likes

Thanks for the code, but can you explain what each part is doing? I’m sort of lost at this part and onwards.

3 Likes

Sure, we could generalize it more applying the classic math.randomseed(tick()) xD

2 Likes

Maybe not only tick() but it’s already better than nothing

2 Likes

I check if the user has got the badge and, if they do, I extend the range from where i pick the random number by the amount of children the secret melee weapon folder has.

Anyway, the number is picked and, if its equal or less than n (the amount of children the normal melee folder has), then the ranth weapon from this folder is picked, people who dont have the badge will always be in this case. If the number is greater than n then that means that “it comes from the extension”, so the (ran-n)th weapon from this folder is picked (ran-n because in this case, its not the final index, but its the final + n).

2 Likes

Oh, wow, that makes so much sense! Thank you so much!

2 Likes

You’re welcome, man. Have a good day.

2 Likes

Hey, I just looked into the code, and I think there was a bit of miscommunication. It doesn’t give quite the result I was looking for. Let me explain what I’m trying to do with an example (please note this example is a bit random)

Let’s say there’s a chicken boss in the game. Beating it gives you a badge, which is intended to add the egg to the players randomizer. The egg is one of two weapons stored in the specialmelee folder, the other weapon being for a completely different boss. What your code does is that it adds both the egg and the other boss’s weapon to your randomizer. I want only the egg to be added.

How would I go about changing your code to better fit this sort of example?

Once again, sorry if the example is a bit random (:wink:)

1 Like

Well, in that case, i would create a table with all the normal weapons and the particular ones added in posteriorly if you have got the required badge for such. This way you would be adding only the particular unlocked weapons.

The following script can be optimized, but its alright.

local char = script.Parent

local plrs = game:GetService("Players")
local plr = plrs:GetPlayerFromCharacter(char)

local bS = game:GetService("BadgeService")
local badge1 = bS:UserHasBadgeAsync(plr.UserId, id) -- id is the ID of your 1st badge
local badge2 = bS:UserHasBadgeAsync(plr.UserId, id) -- id is the ID of your 2nd badge
--etc

local rep = game:GetService("ReplicatedStorage")
local mle = rep:WaitForChild("Melee") -- whatever your melee folder is called
local sMle = rep:WaitForChild("SecretMelee") -- whatever your secret melee folder is called
local wep1 = sMle:WaitForChild("Weapon1")
local wep2 = sMle:WaitForChild("Weapon2")
--etc

local weps = mle:GetChildren()

if badge1 then
    table.insert(weps, wep1)
end

if badge2 then
    table.insert(weps, wep2)
end
--etc

local chosen = weps[math.random(1, #weps)]

local nWep = chosen:Clone()
nWep.Parent = plr.Backpack
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.