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

How can I make a script choose from possabilities and do something with it? [Solved]

Asked by 6 years ago
Edited 6 years ago

To put it simply I have eggs which will hatch into dinosaurs kind of at random but with probabilities. I believe I have the probabilities part is correct but I'm not sure how to make the script do something with the probabilities. What I mean is I dont know how to make the script chose one of the probabilities and make that dino "hatch" from it. This is very important for our game and I couldn't find help anywhere else any help is greatly appreciated. Im sorry for the question but I'm not very experienced scripting.

The script of probabilities

local Dilo = game.Workspace.Dilophosaurus
local Pachy = game.Workspace.Pachy
local Trike = game.Workspace.Triceratops
local Coeluro = game.Workspace.Coelurosaur
local Compy = game.Workspace.Compy


local chances = {
    ['Dilo'] = 20,
    ['Pachy'] = 20,
    ['Trike'] = 10,
    ['Coeluro'] = 20,
    ['Compy'] = 30,
}

local results = {}

table.foreach(chances, function(key, value)
    for i = 1, value do
        table.insert(results, key)
    end
end)

local random = math.random(#results)

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

the simplest way to do this is through fractions. I have tried so hard to find ways but this is the best one here is an example:

local random=math.random(0,100)
local randomchoice

  if random=<50 then
         randomchoice="apple"

  else
         randomchoice="orange"

 end

if you want to go further into making the dinos appear just ask cause i know a good amount about scripting!

here's the another example: i personally like to go to 1000 or even 10,000 for for fractions, because this helps you make rare items

--Dilo = 20%
--Pachy = 20%   
--Trike = 10%
--Coeluro = 20%
--Compy = 30%

local random=math.random(0,100)
local randomchoice

    if random=<20 then
        randomchoice="Dilo"
    elseif random=<40 and random not =<20 then
        randomchoice="Pachy"
    elseif random=<50 and random not =<40 then
        randomchoice="Trike"
    elseif random=<70 and random not =<50 then
        randomchoice="Coeluro"
    elseif random=<100 and random not =<70 then
        randomchoice="Compy"
    end
0
oops! i meant if random<=50 then Zendaya774isstupid 50 — 6y
0
Please indent your code User#19524 175 — 6y
0
sorry i dont really indent my comments so idk how to do it right Zendaya774isstupid 50 — 6y
0
Ok thanks! How would I add more choices, I get above 50 or under 50 or even like 70, 30 but how would i do like 35, 30 and 25 etc JakeDaBeAat 13 — 6y
View all comments (2 more)
0
i'll use your example above to answer this Zendaya774isstupid 50 — 6y
0
ok thanks ill test it out JakeDaBeAat 13 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

So now I changed the script to this, switching =< to <= and instead of and random not I just did and random >=. Example: It chooses a "Pachy" at 25% or less than 40 but greater than 15 which is 25 difference. Credit to zendaya774 for the help.

local random=math.random(0,100)
local randomchoice

    if random <= 15 then
        randomchoice = "Dilo" --15%
        print ("Dilophosaurus Hatched!")
    elseif random <= 40 and random >= 15 then --25%
        randomchoice = "Pachy"
        print ("Pachy Hatched!")
    elseif random <= 55 and random >= 40 then --15%
        randomchoice = "Trike"
        print ("Triceratops Hatched!")
    elseif random <= 70 and random >= 55 then --15%
        randomchoice = "Coeluro"
        print ("Coelurosaur Hatched!")
    elseif random <= 100 and random >= 70 then --30%
        randomchoice = "Compy"
        print ("Compy Hatched!")
    end
0
oh ok, sorry i get mixed up sometimes but thank you for fixing it! Zendaya774isstupid 50 — 6y

Answer this question