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

How should I create a loop that will change Random Number every few seconds?

Asked by 5 years ago

So far, I've created a script inside an NPC or Zombie however you want.

And the point of Script is to give random materials every time when "Zombie" dies.

The script works, but when Zombie dies, it gives me** always** the same material...

So, I know that there's the problem with RandomNumber variable but I have no idea on how to make this to work...

The thing that I've had in my head was: *If a zombie dies that means that script will reset and it will be other numbers... * but obviously it didn't go by that way.

In the following script, I've added marks where the error occurs.


local RandomNumber = math.random(1,5) -- Error here local Humanoid = script.Parent.Humanoid function PwntX_X() local tag = Humanoid:FindFirstChild("creator") if tag ~= nil then if tag.Value ~= nil then local Database = tag.Value:FindFirstChild("Database").Value_Inv if Database ~= nil then if RandomNumber == 1 then Database.Rock_Value.Value = Database.Rock_Value.Value + 1 elseif RandomNumber == 2 then Database.Stick_Value.Value = Database.Stick_Value.Value + 1 elseif RandomNumber == 3 then Database.Plank_Value.Value = Database.Plank_Value.Value + 1 elseif RandomNumber == 4 then Database.Plank_Value.Value = Database.Plank_Value.Value + 1 elseif RandomNumber == 5 then Database.IronIngot_Value.Value = Database.IronIngot_Value.Value + 1 -- Always incerases this value. end wait(0.1) script:Destroy() end end end end Humanoid.Died:Connect(PwntX_X)
0
math.randomseed(tick()) should throw out the first not-so random number, which is a known limitation of lua awfulszn 394 — 5y
0
put that at the top of the script awfulszn 394 — 5y
0
Okay, let me try that AswormeDorijan111 531 — 5y
0
Eh, so will it be like math.randomseed(tick(1, 5)) or AswormeDorijan111 531 — 5y
View all comments (11 more)
0
no, just put it at the top, and keep what you have with math.random() awfulszn 394 — 5y
0
So, like first line would be math.random thing and second line would be with randomseed tick without 1, 5 AswormeDorijan111 531 — 5y
0
no, put math.randomseed() at the top, keep everything you have but move it down 1 line for what I have put. awfulszn 394 — 5y
0
Sec AswormeDorijan111 531 — 5y
0
Ok lemme see AswormeDorijan111 531 — 5y
0
Roblox Studio has stopped responding... -_- AswormeDorijan111 531 — 5y
0
Now it updates only second value... AswormeDorijan111 531 — 5y
0
You should try doing a while true do loop with math.random DAsanicmaster 52 — 5y
0
It's a bad idea, it would never run the rest of the code AswormeDorijan111 531 — 5y
0
after the math.randomseed do for i = 1, 100 do math.random() end User#19524 175 — 5y
0
incapaz your method doesn't work AswormeDorijan111 531 — 5y

1 answer

Log in to vote
1
Answered by
Axdrei 107
5 years ago
Edited 5 years ago

Hello asworme!

Have you tried to call it in the function PwntX_X ?

local Humanoid = script.Parent:WaitForChild("Humanoid")

function PwntX_X() 
    local RandomNumber = math.random(1,5)
    local tag = Humanoid:FindFirstChild("creator") 
    if tag ~= nil then 
        if tag.Value ~= nil then
            local Database = tag.Value:FindFirstChild("Database").Value_Inv
            if Database ~= nil then 
                if RandomNumber == 1 then
                    Database.Rock_Value.Value = Database.Rock_Value.Value + 1
                elseif RandomNumber == 2 then
                    Database.Stick_Value.Value = Database.Stick_Value.Value + 1
                elseif RandomNumber == 3 then
                    Database.Plank_Value.Value = Database.Plank_Value.Value + 1
                elseif RandomNumber == 4 then
                    Database.Plank_Value.Value = Database.Plank_Value.Value + 1
                elseif RandomNumber == 5 then
                    Database.IronIngot_Value.Value = Database.IronIngot_Value.Value + 1
                end
                wait(0.1)
            end
        end 
    end 
end
Humanoid.Died:Connect(PwntX_X) 
0
Thanks, I've didn't even thinked about it :D AswormeDorijan111 531 — 5y
Ad

Answer this question