Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you randomly select an object/variable with different chances?

Hello, this is my first time here. I was wondering if there's a cleaner and better way to write a piece of code that allows the user to have chances to get an item chose, 80% for not rare items, 10% for rare, etc...

I had a friend suggest to do a table and randomly choose one of the things in it but is there anyway to make chances for each thing to get chosen?

		if num > 200 then
		game.Players.LocalPlayer.leaderstats.Crystals.Value = game.Players.LocalPlayer.leaderstats.Crystals.Value + 1
		
		game.Players.LocalPlayer.leaderstats.Geodes.Value = game.Players.LocalPlayer.leaderstats.Geodes.Value - 1
		script.Parent.Parent.TextLabel.TextStrokeColor3 = Color3.new(30,0,30)
				script.Parent.Visible = false
		script.Parent.Parent.No.Visible = false
		game.Players.LocalPlayer.PlayerGui.promptGui.Frame.TextLabel.Text = "You received 1 Crystal!!! Better than none, right?"
		
		wait(3)
		
		script.Parent.Parent.Parent.Parent.Geodes.Frame.ready.Value = "True"
		script.Parent.Parent.Parent:Destroy()
		

		elseif num > 50 and num <201 then
		game.Players.LocalPlayer.leaderstats.Crystals.Value = game.Players.LocalPlayer.leaderstats.Crystals.Value + 2
		
		game.Players.LocalPlayer.leaderstats.Geodes.Value = game.Players.LocalPlayer.leaderstats.Geodes.Value - 1
		script.Parent.Parent.TextLabel.TextStrokeColor3 = Color3.new(90,90,0)
				script.Parent.Visible = false
		script.Parent.Parent.No.Visible = false
		game.Players.LocalPlayer.PlayerGui.promptGui.Frame.TextLabel.Text = "You received 2 Crystals!!! That's pretty rare!!"
		wait(3)
		
		script.Parent.Visible = false
		script.Parent.Parent.No.Visible = false
		script.Parent.Parent.Parent.Parent.Geodes.Frame.ready.Value = "True"
		script.Parent.Parent.Parent:Destroy()
		

		elseif num > 0 and num <51 then
		game.Players.LocalPlayer.leaderstats.Crystals.Value = game.Players.LocalPlayer.leaderstats.Crystals.Value + 5

		game.Players.LocalPlayer.leaderstats.Geodes.Value = game.Players.LocalPlayer.leaderstats.Geodes.Value - 1
		script.Parent.Parent.TextLabel.TextStrokeColor3 = Color3.new(255,0,90)
				script.Parent.Visible = false
		script.Parent.Parent.No.Visible = false
		game.Players.LocalPlayer.PlayerGui.promptGui.Frame.TextLabel.Text = "You received 5 Crystals!!! What a rare find!!"
		wait(3)

1 answer

✓ Accepted answer

Hello. Thanks for coming to scripting helpers!

There are many methods to doing this,I will show you my basic method.

In order to do this,you will need to use three tables: One for the actual objects,and one for the 'Chances',and the last one for the actual storage of the objects once ran with the chance values.

First things first,let's declare the three tables. (Insert parts into workspace named A,B and C)!

Objects={game.Workspace.A,game.Workspace.B,game.Workspace.C}
Chances={12,3,7}
ST={}

Great! But you're not done yet!

Now we need to iterate through the two tables,and 'Copy' the object X amount of times into the 3rd table.

for i,v in pairs(Objects) do
for j=1,Chances[i] do --Getting the chance value associated with the object
table.insert(ST,v) --Inserting the object
end
end

Now,we randomly pick and print the name of the object!

Object=ST[math.random(#ST)]
print(Object.Name.." was picked!")

Please mark this as the answer if it is what you were looking for! I hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.