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)
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
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