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
01 | local rod = script.Parent |
02 | local pomost = game.Workspace.PomostLvl 1 |
03 | local FishOdds = math.random( 1 , 100 ) |
04 | local Fish 1 Odds = 1 , 30 |
05 | local Fish 2 Odds = 31 , 100 |
06 | db = false |
07 |
08 | pomost.Touched:Connect( function () |
09 | rod.Activated:Connect( function () |
10 | db = true |
11 | if FishOdds = = Fish 1 Odds then |
12 | wait( 1 ) |
13 | print ( "Catched fish number 1!" ) |
14 | db = false |
15 | end |
Try doing math.random(1,100) and then get percentage of it, e.g.
01 | --Make a random value between 1 and 100 |
02 | local percentage = math.random( 1 , 100 ) |
03 |
04 | if percentage < = 30 then |
05 | --If percentage is lower than 30, <= 30 |
06 | print ( "<30" ) |
07 | else |
08 | --If percentage is higher than 30, => 30 |
09 | print ( ">30" ) |
10 | end |
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.
01 | local rod = script.Parent |
02 | local pomost = workspace:WaitForChild( "PomostLvl1" ) |
03 | local Fish 1 Odds = { 1 , 30 } --Use tables |
04 | local Fish 2 Odds = { 31 , 100 } |
05 | db = false |
06 |
07 | rod.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 > = Fish 1 Odds [ 1 ] and FishOdds < = Fish 1 Odds [ 2 ] then |
13 | note = "Caught fish number 1" |
14 | elseif FishOdds > = Fish 2 Odds [ 1 ] and FishOdds < = Fish 2 Odds [ 2 ] then |
15 | note = "Caught fish number 2" |
16 | end |
17 | wait( 1 ) |
18 | print (note) |
19 | db = false |
20 | end ) |
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%.
1 | if math.random( 0 , 100 ) < = 30 then |
2 | -- spawn fish1 |
3 | end |
And, since Fish2's spawn rate happens to also be 70%, the probability of Fish1 NOT spawning, we can express both as
1 | if math.random( 0 , 100 ) < = 30 then |
2 | -- spawn fish1 |
3 | else |
4 | -- spawn fish2 |
5 | end |