local Player = game.Players:GetPlayerFromCharacter(script.Parent) local AttackerCooldown = Player:WaitForChild("AttackerCooldown") print(Player) AttackerCooldown.Changed:Connect(function() local Cool = AttackerCooldown.Value while Cool > 0 do Cool = Cool - 1 wait(1) end Player.Attacker.Value = false end)
(StarterCharacterScripts as a ServerScript btw) There are no errors in the output from this script. When it's supposed to become 5, it does. The problem is that this script doesn't count it down from 5 to 0.
Some Fixes/Suggestions
You should use a for
loop to perform professional decremental iteration. It is also suggestion you use :GetPropertyChangedSignal()
to properly hone down on a property of an Instance changing. Your original code will run the while
loop if any aspect of the affiliated instance changes, E.g. it's Name.
Why is it not working?
You allocated AttackerCooldown
‘s Value as a constant, meaning it won’t actually actively modify the ValueObject
, but the variable instead. Anytime you wish to assign something to a given property, you must define the Property, you cannot declare a pathway to it.
On a final note, calling the code above upon a .Changed
signal will cause the signal to fire repeatedly as the pure purpose of the scope is to modify the affiliated Instance. Instead-—as provided by personal conversation of the original-poster—attach the signal to the Attack
ValueObject
so you can run the cool-down upon Attacker
becoming true.
local Player = game.Players:GetPlayerFromCharacter(script.Parent) local Attacker = Player:WaitForChild("Attacker") local AttackerCooldown = Player:WaitForChild("AttackerCooldown") local function CoolDown() if (Attacker.Value ~= true) then return end local CoolState = tonumber(AttackerCooldown.Value) for Cool = CoolState,0,-1 do AttackerCooldown.Value = Cool wait(1) end Attacker.Value = false end Player.Attacker:GetPropertyChangedSignal("Value"):Connect(CoolDown)