Cancel ability to press shift during this animation

  1. What do you want to achieve?
    I want to take the player’s ability to press shift (which is made for sprint) to keep the tiger sitting and not moving while sitting. The walkspeed in the script works as intened, if you press R the tiger sits and can’t move and when you press R again the tiger stands up and you have normal walkspeed (9 in this case).

  2. What is the issue?
    The problem is that when the player presses shift, the tiger changes its walkspeed to 40 which is the new sprint speed and I don’t want the shift key to change the walkspeed during this animation because the tiger is able to move while sitting which I want to avoid.

  3. What solutions have you tried so far?
    I asked in servers, looked on the devforum and watched yt videos but unfortunately I wasn’t able to find something useful.

That’s my code for the sitting animation!

local keyPressed = Enum.KeyCode.R 
local animationId = "7615701980" 

local userInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://"..animationId
local animationTrack = humanoid:LoadAnimation(animation)

userInputService.InputBegan:Connect(function(input,isTyping)
    if isTyping then return end
    if input.KeyCode == keyPressed then
        if not animationTrack.IsPlaying then
            animationTrack:Play()
            humanoid.WalkSpeed = 0
        else
            humanoid.WalkSpeed = 9
            animationTrack:Stop()
        end
    end
end)```

Do you have a special script only for sprinting?

1 Like

Yeah it’s this one!

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer

UserInputService.InputBegan:Connect(function(input, gameProccesed, IsTyping)
	if gameProccesed and IsTyping then
		return
	else
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid = localPlayer.Character:WaitForChild("Humanoid")
			humanoid.WalkSpeed = 40
			
			local animation = Instance.new("Animation")
			animation.AnimationId = "rbxassetid://7599350562"
			animation.Parent = humanoid
			
			loadedAnimation = humanoid:LoadAnimation(animation)
			loadedAnimation:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProccesed, IsTyping)
	if gameProccesed and IsTyping then
		return
	else
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = 9
			loadedAnimation:Stop()
		end
	end
end)```
1 Like

Yeah the script is ignoring the other one, and whenever you press shift it starts sprinting, in the sprinting script add a check to see if it’s sitting, if it isnt then it can sprint

if input.KeyCode == Enum.KeyCode.LeftShift and not sitting then
   -- All of the sprinting stuff, walkspeed, animations
end

Edit: Keep in mind you need to have “sitting” in the script

1 Like

alright, thank you I will try it!

1 Like

Do I have to declare ‘‘sitting’’ because I am using a custom model

1 Like

Yeah you need to declare sitting, there are alot of ways declaring it

1 Like
local sitting = Enum.KeyCode.R``` is that fine?
1 Like

Yeah, but that way it could cause problems since it’s not linked with the other script,

The best way scenario is to move the shift script and implement it with the sitting script, like combine those 2 scripts, so the sitting is the same

1 Like

Okay I will try that, thank you

1 Like

I tried this but it doesn’t work

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local RDown = UserInputService:GetKeysPressed(Enum.KeyCode.R)


UserInputService.InputBegan:Connect(function(input, gameProccesed, IsTyping)
	if gameProccesed and IsTyping and RDown == true then
		return
	else```
The rDown
1 Like

try this

local keyPressed = Enum.KeyCode.R 
local animationId = "7615701980" 

local userInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://"..animationId
local animationTrack = humanoid:LoadAnimation(animation)

local sitting = false

userInputService.InputBegan:Connect(function(input, gameProccesed, isTyping)
	if isTyping then return end
	if input.KeyCode == keyPressed then
		if not animationTrack.IsPlaying then
			animationTrack:Play()
			humanoid.WalkSpeed = 0
			sitting = true
		else
			humanoid.WalkSpeed = 9
			animationTrack:Stop()
			sitting = false	
		end
	end
	
	if gameProccesed and isTyping then
		return
	else
		if input.KeyCode == Enum.KeyCode.LeftShift and not sitting then
			humanoid = player.Character:WaitForChild("Humanoid")
			humanoid.player = 40

			local animation = Instance.new("Animation")
			animation.AnimationId = "rbxassetid://7599350562"
			animation.Parent = humanoid

			loadedAnimation = humanoid:LoadAnimation(animation)
			loadedAnimation:Play()
		end
	end
end)

userInputService.InputEnded:Connect(function(input, gameProccesed, IsTyping)
	if gameProccesed and IsTyping then
		return
	else
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = 9
			loadedAnimation:Stop()
		end
	end
end)```

Its both scripts in one, haven't tested it
1 Like

Okay so in line 51 it says ‘‘Workspace.BIvckghost.LocalScript:51: attempt to index nil with ‘Stop’’’
it’s the line ‘‘loadedAnimation:Stop()’’

Uhh, a local script in a descendant of the workspace?

The local script is in the tiger model which is in a folder called Characters in ReplicatedStorage

Local scripts don’t work when ran in the workspace. (or in a descendant of it)

I am aware, it’s not in workspace

Yea, but if its in the replicatedstorage and it gets parented to the workspace it won’t run

This happens because the sprint animation overrides the other animation
You can check if the animation is playing by using the IsPlaying property.

if animationTrack.IsPlaying then return end

how it would look like:

--it might contain errors, i think you can fix them alone
local animationId = "7615701980" 
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
humanoid = localPlayer.Character:WaitForChild("Humanoid")
local animationTrack = humanoid:LoadAnimation(animation)

UserInputService.InputBegan:Connect(function(input, gameProccesed, IsTyping)
	if gameProccesed and IsTyping or animationTrack.IsPlaying then
		return
    end
    if input.KeyCode ~= Enum.KeyCode.LeftShift then return end

    input.KeyCode == Enum.KeyCode.LeftShift 
	humanoid.WalkSpeed = 40
			
	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://7599350562"
	animation.Parent = humanoid
			
	loadedAnimation = humanoid:LoadAnimation(animation)
	loadedAnimation:Play()
end)

UserInputService.InputEnded:Connect(function(input, gameProccesed, IsTyping)
	if gameProccesed and IsTyping or animationTrack.IsPlaying then
		return
	end
	if input.KeyCode ~= Enum.KeyCode.LeftShift then return end
	humanoid.WalkSpeed = 9
	loadedAnimation:Stop()

end)

I also made the code a little bit cleaner

1 Like

Thank you first of all! I fixed some errors but it sadly when I press shift the speed is 40 instead of 0 at that moment, thank you all for help!