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

Any idea why this script that plays an animation when you press shift not working?

Asked by 5 years ago

Hello! I've been trying for a bit to get this dang issue from happening. Here is the script I am using, this is in StarterCharacterScripts. Also, this is a Local Script, and I have two Remote Events in ReplicatedStorage, and two Server Scripts in ServerScriptService. Hope you can figure out whats going on! -Mrmonkeyman120

local tool = game.StarterPack.M4 
local db = true
local UserInputService = game:GetService('UserInputService')
local anim = Instance.new("Animation")
anim.AnimationId = 'https://www.roblox.com/asset/?id=3345626311'
local playAnim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(anim)
game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer()
UserInputService.InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.LeftShift and db then 
        db = false
        if tool.Equipped then
        playAnim:Play()
        db = true
    end
    end
end)
if tool.Unequipped then
    local Anim = Instance.new("Animation")
    Anim.AnimationId = 'https://www.roblox.com/asset/?id=3345452716'
    local playanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Anim) 
    game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer()
    UserInputService.InputBegan:Connect(function(input, Event)
        if input.KeyCode == Enum.KeyCode.LeftShift and db then
            db = false
            playAnim:Stop()
            wait(0.15)
            playanim:Play()
            print("Worked")
        end
    end)
end
0
in my script line 1, change it to local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character:FindFirstChild("M4") OnaKat 444 — 5y

2 answers

Log in to vote
0
Answered by
Lucke0051 165
5 years ago

The tool will be in the player's character or backpack, if it's equipped it's in the character. Which means that you should check both the backpack and character.

local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character:FindFirstChild("M4")
0
Ah, I see. Mrmonkeyman120 65 — 5y
0
Nope, the same issue, the animation still plays, but it plays even if the tool is unequipped. Mrmonkeyman120 65 — 5y
Ad
Log in to vote
0
Answered by
OnaKat 444 Moderation Voter
5 years ago
Edited 5 years ago

Your problem is on line 17

if tool.Unequipped then

If you want detect when tool is unequipped, you will need function.

tool.Unequipped:Connect(function()

end)

and line 11 too

tool.Equipped:Connect(function()

end)

also change StarterPack to player backpack

Here is the script

local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character.Backpack:FindFirstChild("M4")
local db = true
local UserInputService = game:GetService('UserInputService')
local anim = Instance.new("Animation")
anim.AnimationId = 'https://www.roblox.com/asset/?id=3345626311'
local playAnim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(anim)
game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer()
local key = nil
UserInputService.InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.LeftShift and db then 
        db = false
        key = input.KeyCode
    end
end)
tool.Equipped:Connect(function()
    if key == Enum.KeyCode.LeftShift and db then
        playAnim:Play()
        db = true
    end
end)
tool.Unequipped:Connect(function()
    local Anim = Instance.new("Animation")
    Anim.AnimationId = 'https://www.roblox.com/asset/?id=3345452716'
    local playanim = game.Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(Anim) 
    game.ReplicatedStorage.ShiftToSprintHoldGun:FireServer()
    UserInputService.InputBegan:Connect(function(input, Event)
        if input.KeyCode == Enum.KeyCode.LeftShift and db then
            db = false
            playAnim:Stop()
            wait(0.15)
            playanim:Play()
            print("Worked")
        end
    end)
end)
0
Its saying backpack isn't a valid object of model? Mrmonkeyman120 65 — 5y
0
change it to local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character:FindFirstChild("M4") OnaKat 444 — 5y

Answer this question