Script Ignoring If Statement

Hey, I’m trying to add a cooldownt to a part that gives you points, but when the cooldown is on, it still gives points. Look at the following script:


Line 7 is where it ignores it. If the player’s cooldown value isn’t the parts cooldown value, give the player points. However, when both of the values are the same, it still gives the points. Please help and thanks.

Also sorry if I didn’t provide a lot of info I had to rush

Are you sure the value are not the same? If you print out the PCcooldown and the CValue what does it print out?

Then they aren’t the same, before the if statement print the values for bug testing

print(a) -- player.PCooldown.Value
print(b) -- CValue.Value
if a ~= b then

end

but let’s say they are the same, what I’d do first is make it so when the part gets touched that it’s canTouch property is set to off until you want it to be touched again.

Other than that maybe there is something I’m forgetting.

@StraightScared @LifeDigger
They are both the same

Very likely should not change it (and it will be strange if it does) but if you check if both == each other then would that work rather then checking if they are not the same.

This should work (Works on a timer based system though, if your cooldown system was different notify me)

for _, RewardPiece in ipairs(workspace:GetDescendants()) do
	if RewardPiece.Name ~= "Claim" then continue end

	RewardPiece.Touched:Connect(function(hit)
		if not hit.Parent:FindFirstChild("Humanoid") then return end

		local player = game.Players:GetPlayerFromDescendant(hit.Parent)
		local cooldownValue = player.PCooldown

		if not cooldownValue.Value then return end

		local reward = RewardPiece.Reward
		player.leaderstats.PowerPoints.Value += reward.Value

		cooldownValue.Value = false
		task.wait(0.2) --put the cooldown time
		cooldownValue.Value = true
	end)
end

Also remember this:

Reverse if statements and continue or return early to keep code simpler

Example:
Instead of

if hit.Parent:FindFirstChild("Humanoid") then 
    --stuff here
end

use

if not hit.Parent:FindFirstChild("Humanoid") then return end

--stuff here

Now you might ask as to when use continue instead of return and vice versa.
continue - when if statement is inside a loop
return - when if statement is inside a function

I just used a different method.

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