How can i make this backpack script COMPLETELY random?

Hi people,

I’m currently making a school game and i want to give the player a random backpack upon he joins the game. Unfortunetaly, the math.random isn’t “completely” random, when i spawn in, sometimes i spawn with the default backpack, the purple one, the green one, etc. But there’s a rare chance that they can repeat a BUNCH of times. So the point is: How can i fix this?

This is my code:

local RS = game.ReplicatedStorage
local BackpackFolder = RS:WaitForChild("Backpacks")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local backpacks = BackpackFolder:GetChildren()
		if #backpacks > 0 then
			local randomIndex = math.random(1, #backpacks)
			local randomBackpack = backpacks[randomIndex]:Clone()
			randomBackpack.Parent = Character
		end
	end)
end)

I probably would try to make 2 tables, one with one set of colours. e.g. red, blue and another with another set. e.g. default, green. Then I would randomize a number 1 through 100, if the number is higher than 50 then choose one in the first table if not choose one from the second one.

what? Can you provide my a code example? Also if you’re curious what my explorer looks like, it looks like this:
Screenshot_2

local Backpacktable1 = {"Red", "Default"}
local BackpackTable2 = {"Blue", "Green"}

local Tablechoose = math.random(0, 100)

if Tablechoose > 50 then
local randomIndex = math.random(1, #backpacktable1)
			local randomBackpack = backpacktable1[randomIndex]:Clone()
			randomBackpack.Parent = Character
else
local randomIndex = math.random(1, #backpacktable2)
			local randomBackpack = backpacktable2[randomIndex]:Clone()
			randomBackpack.Parent = Character
end

Something like this, it chooses one in the first table if the number is higher than 50 but if its not it chooses one in the second

1 Like

I would have a rarity system in a module script like this (its tedious but very random)

local BackpackModule = {}

BackpackModule.BackpackRarities = {
   ['BlueBackpack'] = 20
   ['DefaultBackpack'] = 20
   ['GreenBackpack'] = 20
   ['GreenBackpack'] = 20
}
--[[We'll be using a weighted choosing system here. It basically
involves having a random number and having a counter. We will loop
through the rarity table and add the chance to the counter. If the counter
is greater than the number, then we return the backpack :)]]
function BackpackModule.ChooseBackpack()
   local num = math.random(0,80)
   local counter = 0
   for backpack, chance in pairs(BackpackModule.Rarities) do
       counter += chance
       if counter > num then
          return backpack -- Eventually it will reach here
       end
   end
end

Then in your server script…

local RS = game.ReplicatedStorage
local BackpackFolder = RS:WaitForChild("Backpacks")
local BackpackModule = require(game.ServerScriptService.BackpackModule)
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local backpackname = BackpackModule.ChooseBackpack()
        local backpack = BackpackFolder[backpackname]
        backpack.Parent = character
		end
	end)
end)
2 Likes

@MysteryX2076 @JAcoboiskaka1121 Both didn’t worked, @JAcoboiskaka1121 Error was this one: ServerScriptService.RandomBackpack:14: attempt to call missing method 'Clone' of string While @MysteryX2076 error was this one: Module code did not return exactly one value i guess you didn’t return @MysteryX2076

@MysteryX2076 works, the only part he missed is returning the table, so will work but it just forgot 1 line

return BackpackModule
1 Like

ye, and he did a speeling error, but nothing too exaggereted oh my gosh my grammar is terrible

Okay… it is not perfect, but at least it somewhat works

it still keeps repeating sometimes, @MysteryX2076 except i haven’t tested your solution yet, @JAcoboiskaka1121

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