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.
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
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)
@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