Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

0

in my script line 1, change it to local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character:FindFirstChild("M4")

2 answers

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

2 comments

0

Ah, I see.

0

Nope, the same issue, the animation still plays, but it plays even if the tool is unequipped.

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)

2 comments

0

Its saying backpack isn't a valid object of model?

0

change it to local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("M4") or game.Players.LocalPlayer.Character:FindFirstChild("M4")

This discussion is preserved as it was. New answers, comments, and votes are closed.