Os.clock() cooldown not working perfectly

Hey! I’m trying to make a cooldown for a pit maneuver system, by using the os.clock() feature. I’m not really sure how to implement it correctly, as I don’t have much experience working with it.

The value is being set to (saved) an attribute in the Player properties. It’s basically meant to work like this: if you successfully pit maneuver somebody, you won’t be able to pit maneuver anybody again for 2 minutes.

-- Variables
local ActiveVehiclesFolder = workspace:WaitForChild("InteractionObjects"):WaitForChild("VehicleSpawn"):WaitForChild("ActiveVehicles")

game:GetService("Players").PlayerAdded:Connect(function(player)
	player:SetAttribute("oldClock", os.clock()) -- Creating an attribute value for the pit maneuver cooldown
end)

-- A new car has been spawned
ActiveVehiclesFolder.ChildAdded:Connect(function(child)
	if child:IsA("Model") and child:WaitForChild("Body"):FindFirstChild("PitBoxes") then -- Assure that it's a civilian vehicle
		for i, box in pairs(child.Body:WaitForChild("PitBoxes"):GetChildren()) do -- Get all the pit boxes individually
			box.Touched:Connect(function(hit)
				if hit:IsA("BasePart") and hit.Name == "PitBoxHitreg" then -- Ensure that they were hit by a LEO vehicle with the rambar
					if child.DriveSeat.Occupant ~= nil then -- Ensure that the civilian vehicle is being driven
						
						local pittingPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent.Parent.DriveSeat.Occupant.Parent)
						local pittedPlayer = game:GetService("Players"):GetPlayerFromCharacter(child.DriveSeat.Occupant.Parent)
						
						if os.clock() - pittingPlayer:GetAttribute("oldClock") <= 120 then -- Cooldown check
							if hit.Parent.Parent.DriveSeat.Velocity.Magnitude >= 15 then -- Ensure that the pitting vehicle is moving, as well as over a certain speed
								local AngularVelocityValue
								
								pittingPlayer:SetAttribute("oldClock", os.clock()) -- Resetting pit maneuver cooldown

You could try using os.time() or tick() instead as it’s much more direct

1 Like

Well I don’t think that would help tho, it’s about the implementation.

I think you’ve flipped the operator the wrong way.

os.clock() - pittingPlayer:GetAttribute("oldClock") <= 120


Inference Testing
n - m <= 120
0 - 120 <= 120true
120 - 120 <= 120true
241 - 120 <= 120false

By default, the statement is always true during runtime.

1 Like

Ye that’s true, thanks, but it still won’t help if I’m right. I think the problem is with the attribute setting, where I’m already setting the old os.clock() before they even pit somebody, so basically I already start the cooldown from there ig?

Though I can’t just set it to “0” at spawn, because it then returns an error, as I can’t deduct a real number from os.clock(), I assume.

I highly doubt there’s a problem with that attribute setting, unless you are somehow operating this under LocalScript context which it shouldn’t.

The answer you’re actually looking for is the operator being incorrectly set, it should be >=.

I am not sure if you are actually uncertain or have tried the code at least.

I did try the code, I actually tried flipping the operator before this post. It did help with the pitting maneuver itself not working at all (during the other operator, it just didn’t work cuz it instantly got cooldowned), but now, you can basically do the maneuver infinite times, without any cooldown.

Oh nvm, actually flipping the operator makes it not work at all. So it’s currently getting cooldowned from the beginning ig. (when using “<” it can be done infinite times as I said before incorrectly)

Alright, so… alongside other things I had to edit (had to rework the script a lot), thanks @anon81993163 and @MysteryX2076 for your help! It now works perfectly.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.