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

How to make math.random give me a different number?

Asked by 4 years ago
Edited 4 years ago

I am trying to make zombies spawn in random locations (there are 4 at the moment) and they always spawn at the same place. I tried making this custom function to try to make numbers more random but it is not working.

Every time I do a new test session in studio the number changes but it always repeats the same number.

function GetRandom(Num1, Num2)
    local Nums = {}
    math.randomseed(tick())
    for i = 1,100 do
        local Math1 = math.random(Num1,Num2)
        Nums[i] = Math1
    end
    local Math2 = math.random(1,100)
    return Nums[Math2]
end

module.PrepareZombie = function(Name)
    local Zombie = Zombies[Name]:Clone()
    Zombie.Parent = ZombieFolder
    local Spawn = GetRandom(1,#Spawns:GetChildren())
    print(Spawn)
    Spawn = Spawns:GetChildren()[Spawn]
    Zombie.HumanoidRootPart.CFrame = Spawn.CFrame
    ZombieCore.Reincarnate(Zombie)
end
0
Roblox naturally gives you random values by generating random seeds. SmartNode 383 — 4y
0
There is no reason whatsoever for that function to exist. Does math.random(1, #Spawns:GetChildren()) not work? whenallthepigsfly 541 — 4y
0
As a side note, randomizing a set of random numbers doesn't make your random more random. Random? whenallthepigsfly 541 — 4y
0
Are you using the module PrepareZombie function in a server script? If you are, try putting math.randomseed(tick()) at the top instead of in the GetRandom function. Try removing it from the function and putting it in the script you’re using PrepareZombie in as one of the first lines. In all of my scripts needing to use math.random, I always put math.randomseed(tick()) at the top before anything ScrubSadmir 200 — 4y

1 answer

Log in to vote
0
Answered by
Abandion 118
4 years ago
Edited 4 years ago

Use math.randomseed( tick() )

math.randomseed() sets the "seed" for math.random. As long as this number is different, your values will be.

tick() fetches the number of seconds elapsed since January 1st 1970. This will always be a different number, so your list of numbers will always be unique.

0
He did use math.randomseeed() Vinceberget 1420 — 4y
Ad

Answer this question