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

How to make a key cause delay from low bar?

Asked by
fr2013 88
5 years ago
Edited 5 years ago

So I'm trying to make a Magic script where when you press E it would make a Fireball/Create a forcefield but I want it to be used when limited so when a player presses the key once/twice, the bar would go down until it reaches lower than the required power for the magic, so it can delay that key or some sort while it refills until it's available. But it doesn't work very well for some reason.

local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local player = game.Players.LocalPlayer

local power = 10

local debounce  = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character

-----------------------------------------------------------------

UIS.InputBegan:connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.E and gameProcessed == false then
        debounce = true
        while power > 0 and debounce do
            power = power - 0.5
            Bar:TweenSize(UDim2.new(1, 0, power / 10, 0), 'Out', 'Quint', .1, true)
            wait()
            if power <= 0 then
                delay(5,key)
            end
        end
    end
end)

UIS.InputEnded:connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.E and gameProcessed == false then
        debounce = false
        while power < 10 and not debounce do
            power = power + .025
            Bar:TweenSize(UDim2.new(1, 0, power / 10, 0), 'Out', 'Quint', .1, true)
            wait()
            if power <= 0 then
                delay(5,key)
            end
        end
    end
end)
0
delay(5, key) would be an error. delay expects a function. User#19524 175 — 5y
0
So do I just make a function in InputBegan and InputEnded? fr2013 88 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You should try using tick() this is a roblox function that returns how many seconds its been since 1970.

For example

local lastTick
if tick()-lastTick >= 5 or lastTick == nil then
    print("tada")
    lastTick = tick()
end
Ad

Answer this question