Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

This IntValue will not count down through the script, but constantly displays it as 5?

Asked by 4 years ago
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.

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
Contact my discord @Ziffixture#0152 so I can speak easier with you if you encounter any issues. Ziffixture 6913 — 4y
Ad

Answer this question