Chance / Percentage Script

I wrote this code myself this is a chance script like 30% chance of getting a sword

image

10 Likes

What is the issue with the script.

1 Like

I am asking if this script is good? if its a percentage script or no?

in other words if its accurate

1 Like

It seems good enough, if it all adds up to 100 then it should be good. Make sure you sort each chance by how OP it is.

1 Like

wdym? also how do i make it so some chances are like 0.1 or 0.001 since it has to add up to 100 it will be a pain in the **** to calculate it

1 Like

The script looks good but you should move the post #help-and-feedback:code-review.

1 Like

What part are you confused about?

“Make sure you sort each chance by how OP it is”

As an example, for a game like Speed-Run 4, the SpeedBoost would be very “OP” as the game is based off of “Speed”, so it should be the rarest. With other games like obbies, the JumpBoost would be very “OP” as it helps your character navigate and complete the obby easier, so it should be the rarest.

so what should i do? can u show me a code example?

As long as your Chances list is ordered from Highest to Lowest this should work just as well.

local Chances = {
	SpeedBoost = 60,
	JumpBoost = 32,
	Regen = 7,
	LowGravity = 1
}

script.Parent.MouseButton1Up:Connect(function()

	local chance = math.random(0,100)

	for ability,percent in pairs(Chances) do
		if (chance >= percent) then
			print("Ability:",ability,"rolled:",chance)
			break
		end
	end

end)

Speed Run

local Chances = {
SpeedBoost = 10
JumpBoost = 20
Regen = 40
LowGravity = 30

Obbies

local Chances = {
SpeedBoost = 30
JumpBoost = 10
Regen = 55
LowGravity = 5

also how can i make it so one of the chances are like 0.1 or 0.001

If a random number between 0 and 100 isn’t granular enough you could do between a great figure, and then divide to a 100 max figure.

e.g.

local rand100 = math.random(0,100) -- 0 to 100
local rand1000 = math.random(0,1000)/10  --0, 0.1 to 100.0
local rand10000 = math.random(0,10000)/100 -- 0, 0.01 to 100.00

Also a note on my previous code - the for loop I used doesn’t work as I expected and requires a table sort to ensure the list is ordered as you like, you can learn about table.sort here:
table (roblox.com)

By the way - the logic I implemented was set up to mimic and reduce your code, however, if you want the ability to not get any power up until you hit a certain chance - you could just do this;


local Chances = {
	"SpeedBoost",
	"JumpBoost",
	"Regen",
	"LowGravity"
}

local MissChance = 5 -- reduce this to 0 and it will always hit an ability - really this is probably a  better solution to my original suggestion.. ^_^"

local chance = math.random(1,#Chances + MissChance)

local ability = Chances[chance]
print("Miss Chance Ability:",ability,"Chance:",chance)
2 Likes

so if i have things that are 0.1 or 0.001 or etc

instead of doing math.random(1,100) i do math.random(1/10000) / 100? or math.random(1/1000) / 100 or math.random(1/100) / 100

also when it picks a num like 53.1 or 53.24 and will my script with the signs < > = work?

will it compare?

<> is SQL for Not Equal to, right?

The equivalent in Lua is ~=

This wouldn’t work as expected, and neither of the solutions proposed in this post would.

Since I suppose that you want it to always end up choosing one, I would do something like @WingItMan did, but more like this:

local Chances = {
	SpeedBoost = 60,
	JumpBoost = 32,
	Regen = 7,
	LowGravity = 1
}

script.Parent.MouseButton1Up:Connect(function()

    local chance = math.random(0,100)
    local accum = 0
    
    for ability, percent in pairs(Chances) do
        if chance <= percent + accum then
            print(ability)
            break
        end
        accum += percent
    end

end)

Instead of checking if chance >= percent, do the opposite, and sum the chances.

You can also do it with if statements this way:

local choosenchance = math.random(1, 100)
if choosenchance <= Chances.SpeedBoost then
    print("SpeedBoost")
elseif choosenchance <= Chances.SpeedBoost + Chances.JumpBoost then
    print("JumpBoost")
elseif choosenchance <= Chances.SpeedBoost + Chances.JumpBoost + Chances.Regen then
    print("Regen")
else
    print("LowGravity")
end
1 Like

so ur saying my script wont work?

Your script would work, but it won’t be accurate.

The method proposed by @WingItMan won’t work, or it would be very innacurate.

1 Like