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

math.random picks same outcome every time? [Solved]

Asked by 6 years ago
Edited 6 years ago

Let me just start by saying if you moderate this question I am just gonna keep asking it. "Question already has an answer" https://scriptinghelpers.org/questions/28122/having-mathrandom-issue-solved

That question does NOT answer nor work with my problem. So the question I am asking has not been solved. Maybe if you asked if it worked before instantly moderating my question.

That question does not solve my problem. "Add a wait at the start of the function" it does not solve my problem.

Ill ask again, my script picks the same outcome over and over. Here is the code

Its under ReplicatedFirst and its a Local Script

local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
PlayerGui:SetTopbarTransparency(0)

local screen = Instance.new("ScreenGui")
screen.Parent = PlayerGui

local textLabel = Instance.new("TextLabel")

local num = math.random(1,2)
    if num == 1 then
        LoadingText = "Loading"
        textLabel.BorderSizePixel = 0
        textLabel.Size = UDim2.new(1, 0, 1, 0)
        textLabel.Font = "Garamond"
        textLabel.FontSize = Enum.FontSize.Size28
        textLabel.Parent = screen

    elseif num == 2 then
        LoadingText = "Beep"
        textLabel.BorderSizePixel = 0
        textLabel.Size = UDim2.new(1, 0, 1, 0)
        textLabel.Font = "Garamond"
        textLabel.FontSize = Enum.FontSize.Size28
        textLabel.BackgroundColor3 = (Color3.new(0, 255, 255))
        textLabel.Parent = screen
end

local textLabel2 = Instance.new("TextLabel")
textLabel2.Text = "I wonder what game hint will go here?"
textLabel2.BorderSizePixel = 0
textLabel2.Size = UDim2.new(1, 0, .25, 0)
textLabel2.Position = UDim2.new(0, 0, .55, 0)
textLabel2.Font = "Garamond"
textLabel2.FontSize = Enum.FontSize.Size28
textLabel2.Parent = screen

script.Parent:RemoveDefaultLoadingScreen()

local count = 0
local start = tick()
while tick() - start < 6 do
    textLabel.Text = LoadingText .. string.rep(".", count)
    count = (count + 1) % 4
    wait(.5)
end

screen.Enabled = false

Like my real question gets moderated and this doesnt - https://scriptinghelpers.org/questions/48698/can-someone-help-me-with-a-kamehameha-base-to-go-off-of

Thanks.

0
Did you ever read my edit? I posted it on your question so you should've got a notification. It is based off what your system is doing at that time. hiimgoodpack 2009 — 6y
0
You never posted and edit, all you did was link me to a year old post that didnt work for what I was trying to do. GottaHaveAFunTime 218 — 6y

2 answers

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

That's because math.random() uses a seed. This seed generates a sequence of numbers. The sequence is generated using a pseudo random number generator based of a seed. To change a seed we simply just need to use math.randomseed(). This allows you to change the seed that math.random runs on. To make sure we always generate random numbers each game, we can use the function tick(). So all we need to simply do is math.randomseed(tick()). All you need to do is put this ONCE in your code at the very start. So do the following:

math.randomseed(tick())
--rest of your code

I hope this helps, if it did please accept!

0
This still doesnt work, it still picks option 2 everytime. GottaHaveAFunTime 218 — 6y
0
make usre you run a few times abnotaddable 920 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago

I think I know why it is picking the same number. It is directly on the LocalScript, and it will execute the random code automatically. If you put the math.random() in a function, or a Connect() function or any kind of function, it will probably work.

But this is what I think, just wait for a while until another helper comes and answer your question :).

[NOTE: if it does not work with the function thing, try to extend the maximum number!]

0
what????? so confusing hiimgoodpack 2009 — 6y

Answer this question