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

Is there any better methods than the ones im currently using?

Asked by 2 years ago

I need help making a cooldown indicator system similar to A Universal Time, where they appear and disappear whenever you use a move and whenever they are finished and you can use the move again, and also new cooldowns are added under the current ones and move up when the gui is no longer visible.

This is the code Im using currently, it works now but i know Im going to have issues when trying to script in more guis and make them all work, i just need to figure out if there are different and more efficient methods i can use, and this is only for two cooldowns...

local plr = game:GetService("Players").LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local cs = game:GetService("CollectionService")
local plrgui = plr.PlayerGui
local barragecd = plrgui.Cooldowns.barrage
local dashcd = plrgui.Cooldowns.dash
while true do 
    if cs:HasTag(chr,"BarrageCD") and not barragecd.Visible == true then
        barragecd.Visible = true
        barragecd.cd:TweenSize(UDim2.new(0,0,0,11),"In","Linear",6,true)
        delay(6, function()
            barragecd.Visible = false
            barragecd.cd:TweenSize(UDim2.new(0, 200,0, 11),"In","Linear",0.01,true)
        end)
    end
    if cs:HasTag(chr, "ROLLCD") and not dashcd.Visible == true then
        dashcd.Visible = true
        dashcd.cd:TweenSize(UDim2.new(0,0,0,11),"In","Linear",6,true)
        delay(6, function()
            dashcd.Visible = false
            dashcd.cd:TweenSize(UDim2.new(0, 200,0, 11),"In","Linear",0.01,true)
        end)
    end
    if dashcd.Visible == true and barragecd.Visible == false then
        barragecd:TweenPosition(UDim2.new(0.93, 0,0.264, 0),"In","Linear",0.01,true)
        dashcd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
    elseif dashcd.Visible == false and barragecd.Visible == true then
        dashcd:TweenPosition(UDim2.new(0.93, 0,0.264, 0),"In","Linear",0.01,true)
        barragecd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
    elseif dashcd.Visible == false and barragecd.Visible == false then
        barragecd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
        dashcd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
    end
    wait()
end

1 answer

Log in to vote
0
Answered by
Psudar 882 Moderation Voter
2 years ago
Edited 2 years ago

If you don't want to change the structure of your code so that you continue using an infinite while loop, you can clean up a lot of the copied code by creating a function like this:

local function animateCooldown(tag, gui)
    if cs:HasTag(chr,tag) and not gui.Visible == true then
        gui.Visible = true
            gui.cd:TweenSize(UDim2.new(0,0,0,11),"In","Linear",6,true)
        delay(6, function()
        gui.Visible = false
                gui.cd:TweenSize(UDim2.new(0, 200,0, 11),"In","Linear",0.01,true)
        end)
    end
end

This would shorten your code to something like this:

while true do 
    animateCoolDown("BarrageCD", barragecd)
    animateCoolDown("ROLLCD", dashcd)

    if dashcd.Visible == true and barragecd.Visible == false then
        barragecd:TweenPosition(UDim2.new(0.93, 0,0.264, 0),"In","Linear",0.01,true)
        dashcd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
    elseif dashcd.Visible == false and barragecd.Visible == true then
        dashcd:TweenPosition(UDim2.new(0.93, 0,0.264, 0),"In","Linear",0.01,true)
        barragecd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
    elseif dashcd.Visible == false and barragecd.Visible == false then
        barragecd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
        dashcd:TweenPosition(UDim2.new(0.93, 0,0.187, 0),"In","Linear",0.01,true)
    end
    wait()
end

However, instead of doing a giant loop, which is hard to update and is actually really inefficient sometimes, you should continue using CollectionService. However, you can use a function from the CollectionService called GetInstanceAddedSignal that will return a signal for you to connect a function to. It works similar to Instance:GetPropertyChangedSignal() if you've ever used that before.


-- this is the code you want to execute whenever a tag is added local function onTagAddedToCharacter(character, tag, gui) if not gui.Visible then -- do all the animations end end -- since you're adding the tag to the character, we can get it here CollectionService:GetInstanceAddedSignal("BarrageCD"):Connect(function(character) -- now we can call that function we just wrote onTagAddedToCharacter(character, "BarrageCD", barragecd) end) -- we can do the same thing but for a different tag CollectionService:GetInstanceAddedSignal("ROLLCD"):Connect(function(character) -- now we can call that function we just wrote onTagAddedToCharacter(character, "ROLLCD", dashcd) end)

I didn't write all of the code necessary to transition your current implementation to this, but I hope this gives you an idea of how to solve the problem :) You can shorten this, even more, to eliminate redundancy with a table of tags and for loops, but for now, this is a good place to start.

0
thank you, i will try to implement this GodOfTheMuItiverse 2 — 2y
Ad

Answer this question