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

Delay for pressing E on a tool + GUI?

Asked by 3 years ago

Hello! I'm making a tool and it plays an animation when pressing E. How can I set a custom delay and make a gui to show that delay?

When I press E, I want the player to have a 5 second delay to press E once again, and show in a gui a second delay or a bar that decreases (Like you'd see in games like Blox Fruit)

Here's the script for my E script (inside the tool)

local player = game.Players.LocalPlayer
local Tool = script.Parent 

local mouse = player:GetMouse()

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

repeat wait(1) until player.Character

local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local AnimE = Instance.new("Animation")
AnimE.Name = "AnimE"
AnimE.Parent = script.Parent

AnimE.AnimationId = "http://www.roblox.com/asset/?id=" .. "6757120886"
local animEtrack = humanoid:LoadAnimation(AnimE)

mouse.KeyDown:connect(function(key)
    if key == "e" then
        animEtrack:Play()
    end
end)

2 answers

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

I suppose you know how to use debounces? To start I'll add a debounce for you.

local player = game.Players.LocalPlayer
local Tool = script.Parent 

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local debounce = false -- New variable
local waitTime = 5 -- New variable

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local AnimE = Instance.new("Animation")
AnimE.Name = "AnimE"
AnimE.Parent = script.Parent

AnimE.AnimationId = "http://www.roblox.com/asset/?id=6757120886" -- You can do the whole link at once.
local animEtrack = humanoid:LoadAnimation(AnimE)

UserInputService.InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.E and not gameProcessed and not debounce then
        debounce = true
        animEtrack:Play()

        wait(waitTime)
        debounce = false
    end
end)

Here is a page about debounces on Roblox's Developer page. So this code makes you able to only play the animation every 5 seconds. And we use UserInputService to get the keypress as well as the gameProcessed Parameter instead of mouse.

Now if you want a Gui to shrink or something like that to indicate the time, then you could tween it. So now I will just assume you have a Gui Frame that you want to tween.

local player = game.Players.LocalPlayer
local Tool = script.Parent 

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local debounce = false -- New variable
local waitTime = 5 -- New variable
local frame = player.PlayerGui:WaitForChild("ScreenGui").FrameToTween -- Set this path to your Frame

local function debounceToFalse(didComplete) -- New function
    if didComplete then
        debounce = false
    else
        print("Something went wrong")
    end
end

local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local AnimE = Instance.new("Animation")
AnimE.Name = "AnimE"
AnimE.Parent = script.Parent

AnimE.AnimationId = "http://www.roblox.com/asset/?id=6757120886" -- You can do the whole link at once.
local animEtrack = humanoid:LoadAnimation(AnimE)

UserInputService.InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.E and not gameProcessed and not debounce then
        debounce = true
        animEtrack:Play()
        frame.Size = UDim2.new(0, 0, 0, 50)

        frame:TweenSize(
            UDim2.new(0, 100, 0, 50),
            Enum.EasingDirection.In,
            Enum.EasingStyle.Linear,
            waitTime,
            true,
            debounceToFalse
        )
    end
end)

And here is a link to a page about tweening the size of Gui Objects.

I hope this helped! Please comment if there is anything you're wondering or something that isn't working.

0
Hey! The frame doesn't seem to reduce it's size and the animation doesn't seem to play, is there anything wrong with what I made? I put my frame and also, there's Udim2 but it should be UDim2, can't seem to fix it qMrSpooky 20 — 3y
0
I mean I tried changing the UDim2 back and forth (capital letter for D and lowersized) or tried changing my Frame or something, but it still doesn't work qMrSpooky 20 — 3y
0
Yes of course sorry, it should be UDim2.new() Spjureeedd 385 — 3y
0
Do you get any errors? Spjureeedd 385 — 3y
View all comments (2 more)
0
I updated the script after trying it myself. Now this version works for the gui, just make sure you set the path of the variable correctly with all names and such. I don't know why your animation doesn't work though. Nothing happened when I tried and I even printed ananimEtrack.IsPlaying to see if it was playing, and it returned true, so I don't know what's wrong Spjureeedd 385 — 3y
0
It only works for an animation that you created, I thinnk. qMrSpooky 20 — 3y
Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago

Try this

local player = game.Players.LocalPlayer
local Tool = script.Parent 

local mouse = player:GetMouse()

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

repeat wait(1) until player.Character

local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local AnimE = Instance.new("Animation")
AnimE.Name = "AnimE"
AnimE.Parent = script.Parent

AnimE.AnimationId = "http://www.roblox.com/asset/?id=" .. "6757120886"
local animEtrack = humanoid:LoadAnimation(AnimE)

local debounce = false

UserInputService.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.E then
        if debounce == false then
            debounce = true
            animEtrack:Play()

            wait(3) -- Amount of time you want to wait until they are done

            debounce = false

        end
    end
end)
0
This fixed 80% of my problems. Question is, how do I put that delay to display in a GUI? qMrSpooky 20 — 3y
0
Make a Gui and make a Timer for 3 seconds and make that fire when the delay is happening MattVSNNL 620 — 3y

Answer this question