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

How to trigger my animation here without using wait()?

Asked by
soutpansa 120
7 years ago

I need an animation I made to trigger when this button is pressed, and it works by using:

local animTrack = hum:LoadAnimation(script.Punch)
        animTrack:Play()
        wait(0.6)
        animTrack:Stop()

But it messes up the cooldown on the script, and allows the skill to be used 2 times in a row before the cooldown happens. Anyone have any ideas?

local Player = game.Players.LocalPlayer 
local mouse = Player:GetMouse() 
local lastUse = 0;
local char = Player.Character 
local hum = char.Humanoid



local function onKeyDown(key) 
    local Key = key:lower()
    if key == "z" and Player.Levelss.Level.Value >= 15 and tick()-lastUse > 10 and Player.Levelss.RangedPunch.Value < 1  then 
        local x = Instance.new("Part")
        x.BrickColor = BrickColor.new("White")
        x.Material = "Neon"
        x.CanCollide = false
        x.Transparency = 0.6
        x.Size = Vector3.new(10,10,10)
        x.TopSurface = "Smooth"
        x.BottomSurface = "Smooth"
        x.Shape = "Ball"
        x.Name = Player.Name
        local fd = script.fireDamage:clone()
        fd.Parent = x
        local a = Instance.new("Sound")
        a.SoundId = "http://www.roblox.com/asset/?id=260430117"
        a.Parent = x
        a.Volume = 2
        a:play()


        Player.Levelss.XP.Value = Player.Levelss.XP.Value + 8


        local y = Instance.new("BodyVelocity")
        y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge)
        y.velocity = Player.Character.Torso.CFrame.lookVector*95

        x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 1 , -10)
        x.Parent = Workspace
        y.Parent = x    
        game.Debris:AddItem(x, 6)

        lastUse = tick(); 
    end
end


mouse.KeyDown:connect(onKeyDown)

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

If you don't want the code to yield or stop your code in this function, I suggest using spawn().

spawn(function()
    animTrack:Play()
    wait(0.6)
    animTrack:Stop()
end)

Apart from that, KeyDown is deprecated. I suggest using UserInputService instead.

Here's an example:

--// Services
local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer 
local mouse = Player:GetMouse() 
local lastUse = 0;
local char = Player.Character 
local hum = char.Humanoid

--// Animation:
local animTrack = hum:LoadAnimation(script.Punch)



local function onKeyDown(inputObject, GPE) 
    if inputObject.KeyCode == Enum.KeyCode.Z and Player.Levelss.Level.Value >= 15 and tick()-lastUse > 10 and Player.Levelss.RangedPunch.Value < 1  then 

        local x = Instance.new("Part")
        x.BrickColor = BrickColor.new("White")
        x.Material = "Neon"
        x.CanCollide = false
        x.Transparency = 0.6
        x.Size = Vector3.new(10,10,10)
        x.TopSurface = "Smooth"
        x.BottomSurface = "Smooth"
        x.Shape = "Ball"
        x.Name = Player.Name

        local fd = script.fireDamage:Clone()
        fd.Parent = x
        local a = Instance.new("Sound")
        a.SoundId = "http://www.roblox.com/asset/?id=260430117"
        a.Parent = x
        a.Volume = 2
        a:Play()

        spawn(function() --\\ Play Animation
            animTrack:Play()
            wait(0.6)
            animTrack:Stop()
        end)

        Player.Levelss.XP.Value = Player.Levelss.XP.Value + 8

        local y = Instance.new("BodyVelocity")
        y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge)
        y.velocity = Player.Character.Torso.CFrame.lookVector*95

        x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 1 , -10)
        x.Parent = Workspace
        y.Parent = x    
        game.Debris:AddItem(x, 6)

        lastUse = tick(); 
    end
end


UIS.InputBegan:Connect(onKeyDown)
This is an example

EDIT:

Maybe a better solution might be moving some things around, though. If you update lastUse before yielding, this will make sure to counter any double-uses of the tool.

--// Services
local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer 
local mouse = Player:GetMouse() 
local lastUse = 0;
local char = Player.Character 
local hum = char.Humanoid

--// Animation:
local animTrack = hum:LoadAnimation(script.Punch)



local function onKeyDown(inputObject, GPE) 
    if inputObject.KeyCode == Enum.KeyCode.Z and Player.Levelss.Level.Value >= 15 and tick()-lastUse > 10 and Player.Levelss.RangedPunch.Value < 1  then 

        lastUse = tick(); --\\ Moved

        local x = Instance.new("Part")
        x.BrickColor = BrickColor.new("White")
        x.Material = "Neon"
        x.CanCollide = false
        x.Transparency = 0.6
        x.Size = Vector3.new(10,10,10)
        x.TopSurface = "Smooth"
        x.BottomSurface = "Smooth"
        x.Shape = "Ball"
        x.Name = Player.Name

        local fd = script.fireDamage:Clone()
        fd.Parent = x
        local a = Instance.new("Sound")
        a.SoundId = "http://www.roblox.com/asset/?id=260430117"
        a.Parent = x
        a.Volume = 2
        a:Play()

        Player.Levelss.XP.Value = Player.Levelss.XP.Value + 8

        local y = Instance.new("BodyVelocity")
        y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge)
        y.velocity = Player.Character.Torso.CFrame.lookVector*95

        x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 1 , -10)
        x.Parent = Workspace
        y.Parent = x    
        game.Debris:AddItem(x, 6)

        animTrack:Play() --\\ Play animation
        wait(0.6)
        animTrack:Stop()

    end
end


UIS.InputBegan:Connect(onKeyDown)
This is an example

I do hope I understood the question correctly.

0
Yes, thank you for taking the time to post that edit, it was very easy to understand and worked great! The only thing I had to change was where animTrack was defined, I had to move local animTrack to be right above where it Plays and stops the animation. Apart from that, you're a life saver! Thanks so much! soutpansa 120 — 7y
0
Are you getting "Huamnoid needs to be a in workspace" or something like that? If so, repeat wait() until char.Parent == workspace. Doing it the way you're doing it isn't good :c OldPalHappy 1477 — 7y
Ad

Answer this question