Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to use percentage, im kinda confused?

Asked by
kapipi12 133
6 years ago
Edited 6 years ago

I made something like percentage for dropping fishes. For example, Fish 1 has 30 % to be dropped but Fish 2 has 70%. I was trying to use math.random but it didn't work. Any suggestions on how to do it( with explanation and modifying percentage). // Edit, another problem

01local rod = script.Parent
02local pomost = game.Workspace.PomostLvl1
03local FishOdds = math.random(1,100)
04local Fish1Odds = 1,30
05local Fish2Odds = 31,100
06db = false
07 
08pomost.Touched:Connect(function()
09    rod.Activated:Connect(function()
10        db = true
11        if FishOdds == Fish1Odds then
12            wait(1)
13            print("Catched fish number 1!")
14            db = false
15        end
View all 22 lines...
0
just do math.random(0,100) and change that to the percentage :3 greatneil80 2647 — 6y
1
I was trying to do it but I wanted to do something like Fish 1 odds: 30, fish 2 odds: 70 kapipi12 133 — 6y

3 answers

Log in to vote
0
Answered by
NorteX_tv 101
6 years ago

Try doing math.random(1,100) and then get percentage of it, e.g.

01--Make a random value between 1 and 100
02local percentage = math.random(1,100)
03 
04if percentage <= 30 then
05    --If percentage is lower than 30, <= 30
06    print("<30")
07else
08    --If percentage is higher than 30, => 30
09    print(">30")
10end
Ad
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

There are a couple issues here..

  • You are attempting to assign 1 variable to 2 values. You should be using a table.

  • FishOdds has been defined outside of the event; therefore, shall only ever return the same thing. Define inside the Activated event to get different results every time it fires.

  • You are comparing the FishOdds result incorrectly. You have used the == equivalency comparative operator when you should have used >= and <= to specify a spectrum.

  • Your debounce has no condition, you are just toggling a bool which is doing nothing.


01local rod = script.Parent
02local pomost = workspace:WaitForChild("PomostLvl1")
03local Fish1Odds = {1,30} --Use tables
04local Fish2Odds = {31,100}
05db = false
06 
07rod.Activated:Connect(function()
08    if db then return end --Debounce condition
09    db = true
10    local FishOdds = math.random(1,100) --Define inside the event
11    local note = " " -- <= and >= for spectrum
12    if FishOdds >= Fish1Odds[1] and FishOdds <= Fish1Odds[2] then
13        note = "Caught fish number 1"
14    elseif FishOdds >= Fish2Odds[1] and FishOdds <= Fish2Odds[2] then
15        note = "Caught fish number 2"
16    end
17    wait(1)
18    print(note)
19    db = false
20end)
Note: Not sure what you were trying to do with the Touched event..
2
Thank you both, Goulstem and systemise for help :) kapipi12 133 — 6y
Log in to vote
1
Answered by
systack 123
6 years ago
Edited 6 years ago

If we wanted to calculate the probability of landing heads when flipping a coin once, we'd take the number of values that'd meet how condition (landing heads), which in this case is one, and divide it by the number of all possible outcomes, 2; so, we'd represent it as 1/2, 50%.

We want to do the same thing here, so first we'll establish the number of outcomes that'd fufill our condition. In your case, 0-30 for fish1, so 0 <= n <= 30. Next, we need to get a randomly generated number from our range, which in your case is 100, which we can do using math.random(0,100), giving us 100 possible outcomes. So, 1/30 or 30%.

1if math.random(0, 100) <= 30 then
2    -- spawn fish1
3end

And, since Fish2's spawn rate happens to also be 70%, the probability of Fish1 NOT spawning, we can express both as

1if math.random(0, 100) <= 30 then
2    -- spawn fish1
3else
4    -- spawn fish2
5end

Answer this question