Random Timer

Hello, I’ve been making this jumping entity and I want it to jump at different intervals instead of a constant interval. For example, there is a 3 second delay to each jump, so it would be like jump, wait 3 seconds, jump, wait 3 seconds… I want it to be different like jump, wait 2 seconds, jump, wait 5 seconds, jump, wait 4 seconds. I tried the following code ,but it doesn’t work.

timer.Create( "jump_timer"..self:EntIndex(), i, 0, function()
	phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100))
local i = math.random(2,5)

Don’t you have to define ‘I’ before you run the timer?

local i = math.random(2,5)

timer.Create( “jump_timer”…self:EntIndex(), i, 0, function()
phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100))

I did define I in the following code

local i = math.random(2,5)

You defined it after you try to use it.

I know, but you have to put it before the timer. Not after.

Thanks, but I am still not getting what im looking for. I am getting the same random interval. For example
local i = math.random(2,5)
jump, wait 3 seconds, jump, wait 3 seconds, jump, wait 3 seconds, ect.

I want it to be like
jump,wait 2 seconds, jump,wait 5 seconds, jump,wait 3 seconds, jump,wait 4 seconds, jump,

You need to have the timer delete itself and then remake it on every tick as there is no method to make a timer have dynamic increments.

local i = math.random(2,5)

local function JumpTimerResponse()
i = math.random(2,5)
phys:SetVelocity(self.Entity:GetRight() * -100 + Vector(0,0,100))
timer.Create( “jump_timer” … self:EntIndex(),i, 1 );

timer.Create( “jump_timer” … self:EntIndex(), math.random(2,5), 1 );

Something like that.

[lua]function ENT:Initialize()
self.nextJump = CurTime() + math.random(2, 5); – CurTime() is the time the script was ran at in seconds. Adding an amount to it basically means: Amount seconds from now

function ENT:AttemptJump()
if CurTime() >= self.nextJump then
local phys = self:GetPhysicsObject();
if IsValid(phys) then
phys:SetVelocity(self:GetRight() * -100 + Vector(0,0,100));
self.nextJump = CurTime() + math.random(2, 5);

function ENT:Think()

Personally I hate using timers into entities.
I don’t know if it’s just me, but my timers tend to screw up if they are working in a “complex” way.
(Also, this method has the benefit to be able to get how long left until the jump occurs again)

[lua]function ENT:HowLongUntilJump()
return math.Round(self.nextJump - CurTime(), 1); – Rounds it to only one decimal digit, otherwise there might be a lot.

nextJump should be initialized in your entity’s Initialize function, otherwise you might as well initialize it to 0. Other than that, your code is the right way to do it.

I only dislike this part:


The only time this should be used is when you want to separate two instructions on the same line. Or as a replacement for commas in table declarations. A lot of beginners tend to read code like that and then believe that semicolons are an obligation.

I was thinking about putting it into initialize, but I was afraid the Think function would start running before Initialize for some reason. Either way, it has always worked for me the way I do it. (Although I’ll edit the post putting it into initialize for good practice)

Thank you ~Razor~. It works!