Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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) 

15 comments

0

math.randomseed(tick()) should throw out the first not-so random number, which is a known limitation of lua

0

put that at the top of the script

0

Okay, let me try that

0

Eh, so will it be like math.randomseed(tick(1, 5)) or

0

no, just put it at the top, and keep what you have with math.random()

0

So, like first line would be math.random thing and second line would be with randomseed tick without 1, 5

0

no, put math.randomseed() at the top, keep everything you have but move it down 1 line for what I have put.

0

Sec

0

Ok lemme see

0

Roblox Studio has stopped responding... -_-

0

Now it updates only second value...

0

You should try doing a while true do loop with math.random

0

It's a bad idea, it would never run the rest of the code

0

after the math.randomseed do for i = 1, 100 do math.random() end

0

incapaz your method doesn't work

1 answer

✓ Accepted answer

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) 

1 comment

0

Thanks, I've didn't even thinked about it :D

This discussion is preserved as it was. New answers, comments, and votes are closed.