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)
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.
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)