Help with chance system

The chance system I’ve created is working but not as I have intended for it. It’s is only showing the first values such as Rīdoraito, Makejidamashī, Yasei but not the other values that are after that. I’ve put an OR statement in it so it should be working?

**local clicked = 0

script.Parent.MouseButton1Click:Connect(function()
	clicked = clicked + 1
	print(clicked)
	local random = math.random(1,1000)
	if random <= 1 then
		script.Parent.Parent.TextLabel.Text = "Mugen"
		print("Heavenly")
	elseif random <= 5 then 
		script.Parent.Parent.TextLabel.Text = "Makejidamashī" or script.Parent.Parent.TextLabel.Text == "Hasha"
		print("Supreme")
	elseif random <= 50 then
		script.Parent.Parent.TextLabel.Text = "Rīdoraito" or script.Parent.Parent.TextLabel.Text == "Shihai" or script.Parent.Parent.TextLabel.Text == "Ten" or script.Parent.Parent.TextLabel.Text == "Seifuku-sha"
		print("Blessed")
	elseif random <= 250 then
		script.Parent.Parent.TextLabel.Text = "kingu" or script.Parent.Parent.TextLabel.Text == "Meishu" or script.Parent.Parent.TextLabel.Text == "Rūrā" or script.Parent.Parent.TextLabel.Text == "Bushō"
		print("Legendary")
	elseif random <= 500 then 
		script.Parent.Parent.TextLabel.Text = "Yasei" or script.Parent.Parent.TextLabel.Text == "Damasu" or script.Parent.Parent.TextLabel.Text == "Yūkan" or script.Parent.Parent.TextLabel.Text == "Gādo"
		print("Unique")
	elseif random <= 1000 then 
		script.Parent.Parent.TextLabel.Text = "Osoroshī" or script.Parent.Parent.TextLabel.Text == "Seijō" or script.Parent.Parent.TextLabel.Text == "Ikari"
		print("Common")
	end
end)

**
1 Like

When using ‘or’ in a variable it works as this:

local Variable = A or B

If A is false, it will set it to B
However if A is true it will set it to A and discard B

It only sets it to B if A returns nil or is false

so how would I change my code for it to make it so if you for example get a Supreme it will randomly pick one of the options?

1 Like

well If there’s two options you could add

local chance = math.random(1,2)
if chance == 1 then
-- option1
else
-- option2
end
2 Likes

You could run another math.random (or Random.new():NextInteger()) and choose either one of the options based on the value.

2 Likes

im using elseif statements not if statements, i’ve tried that but it just comes out with errors if i put an else statement

look, that chance system isn’t very good, I decided to remake it for you, and fix it!
to add or remove items, add or remove them from the table, to change the chances change them from the pet name in the table

local clicked = 0

local items = {
	["Mugen"] = {
		chance = 1,
		name = "Mugen"
	},

	["Makejidamashī"] = {
		chance = 5,
		name = "Makejidamashī"
	},

	["Hasha"] = {
		chance = 5,
		name = "Hasha"
	},

	["Rīdoraito"] = {
		chance = 10,
		name = "Rīdoraito"
	},

	["Shiha"] = {
		chance = 50,
		name = "Shihai"
	},

	["Ten"] = {
		chance = 5,
		name = "Ten"
	},

	["Seifuku-sha"] = {
		chance = 5,
		name = "Seifuku-sha"
	},

	["Mugenkingu"] = {
		chance = 250,
		name = "kingu"
	},

	["Meishu"] = {
		chance = 250,
		name = "Meishu"
	},

	["Rūrā"] = {
		chance = 250,
		name = "Rūrā"
	},

	["Bushō"] = {
		chance = 250,
		name = "Bushō"
	},
	["Yasei"] = {
		chance = 500,
		name = "Yasei"
	},

	["Damasu"] = {
		chance = 500,
		name = "Damasu"
	},

	["Yūkan"] = {
		chance = 500,
		name = "Yūkan"
	},

	["Gādo"] = {
		chance = 500,
		name = "Rūrā"
	},

	["Osoroshī"] = {
		chance = 1000,
		name = "Osoroshī"
	},

	["Seijō"] = {
		chance = 1000,
		name = "Seijō"
	},

	["Ikari"] = {
		chance = 1000,
		name = "Ikari"
	},
}

function chooseItem(itemTable)
	local TotalWeight = 0

	for i,v in pairs(itemTable) do
		TotalWeight = TotalWeight + v.chance
	end

	local Chance = math.random(1,TotalWeight)
	local Counter = 0

	for i,v in pairs(itemTable) do
		Counter = Counter + v.chance
		if Counter >= Chance then
			return v.name
		end
	end
end

script.Parent.MouseButton1Click:Connect(function()
	clicked = clicked + 1
	print(clicked)
	print(chooseItem(items))	
end)
6 Likes

works perfectly and as intended thank you for taking the time to make a chance system for me

Hey I know this is solved but if you would like a more modular, cleaner solution here you go, assuming that you actually are going to give an item then all you would do is put it in replicated storage like this.
image
Under a folder named “Items” with the corresponding rarities.

local RepS = game:GetService("ReplicatedStorage")
local Items = RepS:WaitForChild("Items")

-- // Range 0-1000 (mess with the settings or values and calc % on your own.)
local Weight = {
	["Common"] = 500, -- 50%
	["Unique"] = 250, -- 25%
	["Legendary"] = 125, -- 12.5%
	["Blessed"] = 75, -- 7.5%
	["Supreme"] = 49, -- 4.9%
	["Heavenly"] = 1 -- 0.1%
}

local TotalValue = 0
for _, num in pairs(Weight) do
	TotalValue += num
end

local function Chance()
	local R = math.random(TotalValue)
	local ResetVal = TotalValue
	local Rarity
	
	for rarity, num in pairs(Weight) do
		ResetVal -= num
		if R >= ResetVal then
			Rarity = rarity
			break
		end
	end
	
	local RarityFolder = Items:FindFirstChild(Rarity)
	local ChosenItem = RarityFolder:GetChildren()[math.random(#RarityFolder:GetChildren())]
	print("Player has won ".. ChosenItem.Name.. ", a ".. Rarity.. " tier item!")
end

-- // test loop
while task.wait(1) do
	Chance()
end
2 Likes

Sorry for the wait, but the system I am aiming towards is a rolling system where if you roll this it will have some spin animation and then give you one of the options from the list. I’ve already made a String Value which corresponds to that called Spirit which will be renamed to whatever is chosen from the chance thing. You don’t need to script this but can you give me some help for how to make this? The answers that everyone has given works but does not meet all the requirements I’m aiming at, thanks.

Just create UI elements corresponding to the selected rarity items/things and when they click on a slot (button) named after the item/thing it gives it to them

Ok I’ll use the advice you have given and try make my own rolling system using your code. This may take a while for me to do so I’ll reply once I’m done. Thanks for the help.

One more question could I use a remote event that fires once the spin animation is finished to tell a script to change the player’s string value?
For example if supreme is chosen it chooses one of the options in the supreme folder which will be remote events and then fire the chosen event?
Thanks.

Here I’ll give you a diagram to help (assuming your using my random weight system

(sorry zoom in for readability)

If you want for a better explanation if you want you can add me on discord and I’ll screenshare and give you a better idea on how to implement this

allusionist#3979