fivem scripting how can i add a cooldown so people can't spam press E if…
- User
fivem scripting how can i add a cooldown so people can't spam press E if isPlayerNearObject(createdObject, 3.0) and IsControlJustReleased(0, 38) and not holdingball then -- 38 is the key code for "E" if not NetworkHasControlOfEntity(createdObject) then local timeout = GetGameTimer() + 2000 NetworkRequestControlOfEntity(createdObject) while (not NetworkHasControlOfEntity(createdObject)) and (GetGameTimer() < timeout) do Citizen.Wait(0) end end local playerRotation = GetEntityRotation(PlayerPedId(), 2) local forwardVector = GetEntityForwardVector(PlayerPedId()) local forceVector = vector3( forwardVector.x * forwardForceIntensity, forwardVector.y * forwardForceIntensity, upwardForceIntensity ) ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end
- Assistant
To add a cooldown for pressing the "E" key, you can use a variable to store the last time the key was pressed and then check if the cooldown time has passed before allowing the action. Here's an example:
local lastPressedTime = 0 local cooldownTime = 5000 -- Adjust this value for desired cooldown duration in milliseconds. if isPlayerNearObject(createdObject, 3.0) and IsControlJustReleased(0, 38) and not holdingball then local currentTime = GetGameTimer() if currentTime - lastPressedTime >= cooldownTime then lastPressedTime = currentTime if not NetworkHasControlOfEntity(createdObject) then local timeout = GetGameTimer() + 2000 NetworkRequestControlOfEntity(createdObject) while (not NetworkHasControlOfEntity(createdObject)) and (GetGameTimer() < timeout) do Citizen.Wait(0) end end local playerRotation = GetEntityRotation(PlayerPedId(), 2) local forwardVector = GetEntityForwardVector(PlayerPedId()) local forceVector = vector3( forwardVector.x * forwardForceIntensity, forwardVector.y * forwardForceIntensity, upwardForceIntensity ) ApplyForceToEntity(createdObject, 1, forceVector.x, forceVector.y, forceVector.z, 0.0, 0.0, 0.0, 0, false, true, true, false, true) end end