Problem with animation of running change when Shift is pressed

Hi everyone, I’m having a problem with player animations. I have created a script for running player with a change of walking animation. Here it is:

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService") 
local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local Running = false
local Debounce = false
local TweenService = game:GetService("TweenService")


UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then	
		
				plr.Character.Animate.walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=16095835637"
				plr.Character.Animate.run.RunAnim.AnimationId = "http://www.roblox.com/asset/?id=16095835637"
				plr.Character.Animate.Disabled = true
				plr.Character.Animate.Disabled = false	
				TS:Create(plr.Character.Humanoid, TweenInfo.new(0.75), {WalkSpeed = 46}):Play()
				TS:Create(plr.Character.Humanoid, TweenInfo.new(0.75), {JumpPower = 50}):Play()
				TS:Create(workspace.CurrentCamera, TweenInfo.new(0.75), {FieldOfView = 95}):Play()
				Running = true
				wait()
		end
	end
end)

	

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
			plr.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://16633357394"
			plr.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://16633357394"
			plr.Character.Animate.Disabled = true
			plr.Character.Animate.Disabled = false
			TS:Create(plr.Character.Humanoid, TweenInfo.new(0.75), {WalkSpeed = 16}):Play()
			TS:Create(plr.Character.Humanoid, TweenInfo.new(0.75), {JumpPower = 50}):Play()
			TS:Create(workspace.CurrentCamera, TweenInfo.new(0.75), {FieldOfView = 70}):Play()
			Running = false
			wait()
		end
	end
end)

The problem itself is interesting because animation issues are not visible on the client, but are visible on the server. Here is a video on behalf of 2 players:

robloxapp-20240429-1158348.wmv (3.1 MB)

robloxapp-20240429-1158396.wmv (2.3 MB)

Player1 was running and his animation broke and Player2 sees it, but Player1 himself does not see it.

I’m not good at writing scripts, so I have no idea how to fix it.

One thing i’ve noticed is that you are using script.Disabled instead of script.Enabled. Try using script.Enabled.

I think what you’re better off doing is have two animations playing at the same time. One run animation and one walk animation, and depending on what state you’re currently in (running or walking) you can change the animation weights from 0 to 1.

  • Running State = set walk animation weight to 0 and run to 1
  • Walking state = Set run animation weight to 0 and walk to 1
  • Ideally you would want both walk and run animations to have the same length
  • You can try to tween these animation weights to have a smooth transition effect from walk to run and vice versa.

Basically how weight works is the higher the value, the more effect the animation takes. Weight values range from 0 to 1. 0 is no effect while 1 is full effect. If you have two animations playing at weight 1 then the animation with higher priority will play (core, idle, movement, action, etc…). So if you have one run animation play at 0.5 weight and one walk animation playing at 0.5 weight, then you’ll have what you could say the motion between both run and walk animations.

Your current method seems really hacky since you’re attempting to re-enable the animate script, this might work sometimes but doesn’t seem like a reliable solution. It might be a bit tricky to get your own animations to work but it would definitely be a cleaner solution.

2 Likes

Yeah, it really works. I tested on 2 animationTrackы and this problem is no longer there. Also, the transition between animations is better. But I know how to change the weight for animationTrack only. Could you please tell me which command to use to change the weight of the animation itself? Besides, these animations are played by the Animate script, not the one that reads the Shift press.
(Sorry I still don’t know much about coding in Lua.)

You can’t change weight of an animation itself since its only a property of AnimationTrack. There are 2 things you could do.

  1. Create your own animation system that listens to Humanoid events and change them accordingly.
  2. Modify the Animate script to change weight from walk to run depending on your walking speed

If you want the modified Animate script, you’ll want to modify the onRunning(speed) function, its on line 594 in the Animate script.

To modify the Animate script follow these steps:

  1. Start a play solo session in studio
  2. Navigate to your Character under Workspace
  3. Copy the Animate script
  4. Stop your play solo session.
  5. Paste your Animate script into StarterPlayer → StarterCharacterScripts. This will ensure that the modified Animate script will overwrite the default Roblox animate script.
  6. Modify function onRunning(speed) on line 594 of the Animate script to this:
function onRunning(speed)
	local movedDuringEmote =
		userEmoteToRunThresholdChange and currentlyPlayingEmote and Humanoid.MoveDirection == Vector3.new(0, 0, 0)
	local speedThreshold = movedDuringEmote and Humanoid.WalkSpeed or 0.75
	
	if speed > speedThreshold then
        --[[
           Animation speed will still speed up/down, but this ensures the walk animation will be in full
           effect when your WalkSpeed is set to minSpeed, and run animation in full effect when
           WalkSpeed is set to maxSpeed. Setting anything in between will still work.
        --]] 

		local maxSpeed = 24 --The max speed your character will run
		local minSpeed = 16 --The min speed your character will run
		local scale = maxSpeed
		
		speed = math.ceil(speed)
		
		if speed >= minSpeed and speed < maxSpeed then
			scale = maxSpeed + minSpeed
		elseif speed >= maxSpeed then
			scale = maxSpeed
		end
		
		playAnimation("walk", 0.2, Humanoid)
		setAnimationSpeed(speed / scale)
		pose = "Running"
	else
		if emoteNames[currentAnim] == nil and not currentlyPlayingEmote then
			playAnimation("idle", 0.2, Humanoid)
			pose = "Standing"
		end
	end
end
1 Like

Thank you very much! I have roughly understood how to proceed and what to change. Now I’m trying to edit the Animate code to change animations. But there is a little inconsistency in your instructions. The Animate script itself has 584 lines, so I changed the function on line 351. Thank you very much again for your help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.