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

How do I make the animation play only when the tool is equipped?

Asked by 9 years ago

I want this animation to play only when the tool is equipped. Currently it plays when ever you just press "y". How do I do this?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local tool = script.Parent
local Mouse = Player:GetMouse()
local kicking = Instance.new("Animation", Humanoid)
kicking.AnimationId = "http://www.roblox.com/item.aspx?id=273619670"




local function KeyDown(key) 
    key = key:lower()
    if key == 'y' then
        local animTrack = Humanoid:LoadAnimation(kicking)
        animTrack:Play() 

    end 
    end





 Mouse.KeyDown:connect(KeyDown)

3 answers

Log in to vote
0
Answered by 9 years ago

You need to make a line that tells the code if the player is holding out the tool then it can play the animation

0
Can you show me an example? 64batsalex 45 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local tool = script.Parent
local Mouse = Player:GetMouse()
local kicking = Instance.new("Animation", Humanoid)
kicking.AnimationId = "http://www.roblox.com/item.aspx?id=273619670"
local ToolEquiped = false



local function KeyDown(key) 
    key = key:lower()
    if key == 'y'and ToolEquiped  then
        local animTrack = Humanoid:LoadAnimation(kicking)
        animTrack:Play() 

    end 
    end


tool.Equiped:connect(function()ToolEquiped = true end)
tool.Unequipped:connect(function()ToolEquiped = true end)

 Mouse.KeyDown:connect(KeyDown)

0
Didn't work. 64batsalex 45 — 9y
0
he spelt equipped wrong btw TabooAntonioiscool 42 — 4y
Log in to vote
0
Answered by 9 years ago

Or you could disconnect a KeyDown function when equipped, than disconnect on unequipped. But i suppose either work. Here's my example:

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent

local key

tool.Equipped:connect(function()
    key = mouse.KeyDown:connect(function()
        print("Holding tool")
    end)
end)

tool.Unequipped:connect(function()
    if key then
        key:disconnect()
        key = nil
    end
end)

But yeah, there are a few ways.

Answer this question