Trying to make a decent shift-to-sprint with custom animation!

  1. What do you want to achieve?

So first I wanted to make a keybind where when you hold shift it would increase your walkspeed and it would play a custom run animation, I’ve also wanted to manipulate the camera to zoom out when shift was held. And I’ve made it! Without any bugs! It was perfect, till…

  1. What is the issue?

…I noticed that you could hold shift when you weren’t actually moving and it would play the animation. I could understand why it was happenning but I couldn’t find the right fix for that, none of the Youtube Vids/ Scripting Helpers commentaries had anything to prevent this issue from happenning, so I tried another method…

  1. What solutions have you tried so far?

I made a script where it would change the speed and the camera of the player so that it seems he’s running faster. It worked. And then I made another script in the Character-Scripts Folder, where I just copied the Animate Script from my character and pasted it, only changing the id from the run animation. Now the problem is, this custom run animation overwrites the walk animation, even when I was walking really slow only the run animation was playing. I though if I could change the normal speed from 16 (Default Walkspeed) to 9 it would automatically play the walk animation, but it didn’t. So here I am, asking for help.

Animate Script

ShiftToSprint Script

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


mouse.KeyDown:connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 22
		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 9
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)
4 Likes

Check this you should check when the humanoid starts walking, if running == true and the speed is higher than 0 then play running animation, else stop it.

4 Likes

Alright, the script is now partially working, it detects the player moving and it also only sprints if the player is holding shift and moving, that’s pretty nice. But now another problem showed up! All the other animations stopped working, the character seems frozen, it only plays the custom runanimation. Is there a way so that not only the custom run animation plays?

2 Likes

Can you show me the code, it’s really strange that all animations stopped.

2 Likes

Oof, how do I put code? I forgot.

2 Likes

To put the code just copy the one in the script, paste it here, select it and click the </> button.

1 Like
local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local VectorZero = Vector3.new(0, 0, 0)
local Walking = false

local function Moved()		
	if Humanoid.MoveDirection ~= VectorZero then
		Walking = true
	else
		Walking = false
	end	
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(Moved)


function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


mouse.KeyDown:connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 and Walking == true then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 20
		
		local Anim = Instance.new('Animation')
  		Anim.AnimationId = 'rbxassetid://4933688981'
 		local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
 		PlayAnim:Play()

		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 11
		PlayAnim:Stop()

		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)
3 Likes

I don’t think it’s a problem of the script, try test it in team test mode, or outside studio, it may be just a bug.

2 Likes

It might be a problem with the animation. In studio only the run animation works, in game (Group game) none of them work. I’ve heard once that the animations have to be exported by you to function correctly, in case it’s a group game, the animations have to be exported by the group. That might fix the issue only for the run animation, but it might not fix for the other default ones.
I will try that.

2 Likes

Strangely, the run animation works in team test mode

2 Likes

Alright, so I was testing with my friend after exporting the animation to the group, it worked, while the other animations didn’t work (even the emotes didn’t worked). I’ve seen games like “Fisty Town” and they have custom animations for every single movement, maybe because of this issue they had to put their own animations and script the whole thing again, idk. I still don’t know what’s the problem with the other animations.

2 Likes

Wait a minute! I just deleted the Animate script (It was disabled) from the studio and it started working in studio!!! Let’s hope that it also works in game!

2 Likes

Thank you so much for taking the time to help me, I really apreciated. :wink:

2 Likes