Code support - Problem with random math

Hello! I need help with ‘random.math’, I need to make the list called ‘Eggs’ the legendary has a 5% chance to appear, epic 15%, rare 30%, and common 50% and then print the chosen one with its value on screen , How can I do that?

Script code:

   -- Eggs types:
    local legendary = script.Parent["Legendary blue"]
    local epic = script.Parent["Epic red"]
    local rare = script.Parent["Rare brown"]
    local common = script.Parent["Common green"]

    -- Eggs list:
    local Eggs = {legendary = 5, epic = 15, rare = 30, common = 50}

    -- Code
    local Player = game:GetService("Players")

    script.Parent.Touched:Connect(function(hit)
    	local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) 
    	if PlayerCharacter then
    		local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
    		Leaderstats.Cash.Value =  Leaderstats.Cash.Value - 10
    		
    		math.random(Eggs)
            print ("the random math value is: "..Eggs)
    	end
    end)

image

1 Like

Replace

           math.random(Eggs)
           print ("the random math value is: "..Eggs)

with

        local Number = math.random(1,100)
     	local chosen
     	if Number <= Eggs.common then
	     	chosen = "common"
     	elseif Number <= Eggs.rare + Eggs.common then
	     	chosen = 'rare'
     	elseif Number <= Eggs.rare + Eggs.epic + Eggs.common then
	     	chosen = 'epic'
	     else
     		chosen = 'legendary'
	     end
         print ("the random math value is: "..chosen)
3 Likes

oh really thank you very much !! I just couldn’t have done something like that

1 Like

no problem dude! Hope your game does well!

1 Like