does someone know how can i add a cooldown to this

redhot69

amongus
addHook("PlayerThink", function(p)
if not p.mo or p.mo.skin ~= "sonic" then return end

if p.shotCooldown == nil then
p.shotCooldown = 0
end

if p.shotCooldown == 0 and (p.cmd.buttons & BT_CUSTOM1) then
local x = p.mo.x
local y = p.mo.y
local z = p.mo.z + 20*FRACUNIT

local proj = P_SpawnMobj(x, y, z, MT_FIREBALL)
if proj then
proj.angle = p.mo.angle
proj.target = p.mo
P_InstaThrust(proj, proj.angle, 20*FRACUNIT)
proj.flags = $ & ~MF_BOUNCE
end

p.shotCooldown = 1
p.lastShotTime = leveltime
end

if p.shotCooldown > 0 and leveltime - p.lastShotTime >= 1 then
p.shotCooldown = 0
end
end)

im trying for a lot of time now and nothing works
 
You're setting p.lastShotTime to leveltime. That means that on the very next tic, leveltime - p.lastShotTime evaluates to 1 (since leveltime increased by 1), which passes the check (1 >= 1) and sets p.shotCooldown back to 0.
Essentially, the code you wrote DOES add a cooldown... of exactly one tic.

The simple solution is to add another number on top of leveltime when setting p.lastShotTime. Literally, just add +TICRATE to that line and you'll have a 1-second cooldown.
You can adjust it however you like from there. The constant TICRATE represents 1 in-game second, so you can use it to form fractions for however precise you want the cooldown to be (just like with FRACUNIT and map coordinates/speed).

Extra tip: It'd probably be a good idea to reset p.lastShotTime to 0 when the player respawns.
 
You're setting p.lastShotTime to leveltime. That means that on the very next tic, leveltime - p.lastShotTime evaluates to 1 (since leveltime increased by 1), which passes the check (1 >= 1) and sets p.shotCooldown back to 0.
Essentially, the code you wrote DOES add a cooldown... of exactly one tic.

The simple solution is to add another number on top of leveltime when setting p.lastShotTime. Literally, just add +TICRATE to that line and you'll have a 1-second cooldown.
You can adjust it however you like from there. The constant TICRATE represents 1 in-game second, so you can use it to form fractions for however precise you want the cooldown to be (just like with FRACUNIT and map coordinates/speed).

Extra tip: It'd probably be a good idea to reset p.lastShotTime to 0 when the player respawns.
thank you!
 

Who is viewing this thread (Total: 0, Members: 0, Guests: 0)

Back
Top