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

How to do i make it every time i click a part it randomly generated a number out of 3?

Asked by 3 years ago

So I'm trying to do a loading scene where it has hints towards the game at the bottom and I have a randomizer script

local num = math.random(1,3) 
local BrickEvent = game.ReplicatedStorage.Events.Gui

BrickEvent.OnClientEvent:Connect(function(plr)
if num == 1 then
          print("you got Critical Hit Fact")
        script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=true

 elseif num == 2 then
         print("you got Stamina Fact")
        script.Parent.ScreenGui.Frame.HintStamina.Visible=true
 elseif num == 3 then
         print("you got Jump Attack Fact")
        script.Parent.ScreenGui.Frame.HintJump.Visible=true

        wait(12)
        script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=false
        script.Parent.ScreenGui.Frame.HintStamina.Visible=false
        script.Parent.ScreenGui.Frame.HintJump.Visible=false
        end
    end)

but after it picks a random one at the beginning every time I click its the first one over and over again it doesn't ever randomize after the first click help?

1 answer

Log in to vote
1
Answered by
lilzy7 68
3 years ago

The reason this doesn't work is because the random number was already chosen before the function. So your num is before the function so that means its randomized and then every time your function runs its always one number because the random number was before the function. To fix this, just put the local variable INSIDE the function, so each time the function runs, a random number is chosen, not the script you have which already has a chosen number before everything, and it stays like that forever.

Fixed Code:

-- move the local num inside the function
    local BrickEvent = game.ReplicatedStorage.Events.Gui

    BrickEvent.OnClientEvent:Connect(function(plr)
local num = math.random(1,3)
-- random number inside function so that every time function runs there's random number
    if num == 1 then
              print("you got Critical Hit Fact")
            script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=true

     elseif num == 2 then
             print("you got Stamina Fact")
            script.Parent.ScreenGui.Frame.HintStamina.Visible=true
     elseif num == 3 then
             print("you got Jump Attack Fact")
            script.Parent.ScreenGui.Frame.HintJump.Visible=true

            wait(12)
            script.Parent.ScreenGui.Frame.HintCriticalDamage.Visible=false
            script.Parent.ScreenGui.Frame.HintStamina.Visible=false
            script.Parent.ScreenGui.Frame.HintJump.Visible=false
            end
        end)

0
thank you ill try this TheMiniMagic_YT 27 — 3y
0
so this works but i keep getting the same one like twice in a row is there a way i can add a chance system so its really hard to get the same one TheMiniMagic_YT 27 — 3y
0
try math.randomseed(tick()) lilzy7 68 — 3y
Ad

Answer this question