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
5 years ago
Edited 5 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

local rod = script.Parent
local pomost = game.Workspace.PomostLvl1
local FishOdds = math.random(1,100)
local Fish1Odds = 1,30
local Fish2Odds = 31,100
db = false

pomost.Touched:Connect(function()
    rod.Activated:Connect(function()
        db = true
        if FishOdds == Fish1Odds then
            wait(1)
            print("Catched fish number 1!")
            db = false
        end
        if FishOdds == Fish2Odds then
            wait(1)
            print("Catched fish number 2!")
            db = false
        end
    end)
end)

0
just do math.random(0,100) and change that to the percentage :3 greatneil80 2647 — 5y
1
I was trying to do it but I wanted to do something like Fish 1 odds: 30, fish 2 odds: 70 kapipi12 133 — 5y

3 answers

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

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

--Make a random value between 1 and 100
local percentage = math.random(1,100)

if percentage <= 30 then
    --If percentage is lower than 30, <= 30
    print("<30")
else
    --If percentage is higher than 30, => 30
    print(">30")
end
Ad
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
5 years ago
Edited 5 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.


local rod = script.Parent
local pomost = workspace:WaitForChild("PomostLvl1")
local Fish1Odds = {1,30} --Use tables
local Fish2Odds = {31,100}
db = false

rod.Activated:Connect(function()
    if db then return end --Debounce condition
    db = true
    local FishOdds = math.random(1,100) --Define inside the event
    local note = " " -- <= and >= for spectrum
    if FishOdds >= Fish1Odds[1] and FishOdds <= Fish1Odds[2] then
        note = "Caught fish number 1"
    elseif FishOdds >= Fish2Odds[1] and FishOdds <= Fish2Odds[2] then
        note = "Caught fish number 2"
    end
    wait(1)
    print(note)
    db = false
end)
Note: Not sure what you were trying to do with the Touched event..
2
Thank you both, Goulstem and systemise for help :) kapipi12 133 — 5y
Log in to vote
1
Answered by
systack 123
5 years ago
Edited 5 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%.

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

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

if math.random(0, 100) <= 30 then
    -- spawn fish1
else
    -- spawn fish2
end

Answer this question