Animation speed value is recieved too late

Below are 2 scripts that both refer to the numbervalue Speedback. In the 2nd script(sprint) it changes the value and the 1st script(animation) then changes the playbackspeed of the animation. The issue is that it changes the speedback.Value outside of the UIS inputbegan function, which means it only plays the animation with correct speed if you are already sprinting. Maybe it’s a very simple fix but I can’t come up with anything myself.

Also, not sure how making some of the sprintscript serverside will affect the possible solution.

Animation Script

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")

local WalkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(WalkAnim)

local uis = game:GetService("UserInputService")
local speed = game.Players.LocalPlayer.valHolder.Speedback

local movementKeys = {
	Enum.KeyCode.W,
	Enum.KeyCode.S,
	Enum.KeyCode.D,
	Enum.KeyCode.A
}

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		for _, keyCode in pairs(movementKeys) do
			if input.KeyCode == keyCode then

				if not walkAnimTrack.IsPlaying then   
					walkAnimTrack:Play()
					print(speed.Value)
					walkAnimTrack:AdjustSpeed(speed.Value)
				end
				break
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local noMovementKeyPressed = true

		for _, keyCode in pairs(movementKeys) do
			if uis:IsKeyDown(keyCode) then
				noMovementKeyPressed = false
				break
			end
		end

		if noMovementKeyPressed then
			walkAnimTrack:Stop()
		end
	end
end)

Sprint Script (planning to use remoteevent to change the values on the server)

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local plr = game.Players.LocalPlayer

local sprintKey = Enum.KeyCode.LeftShift

local uis = game:GetService("UserInputService")
local speed = game.Players.LocalPlayer.valHolder.Speedback
local label = game.Players.LocalPlayer.PlayerGui.Interface.bg.TextLabel

local maxStamina = game.Players.LocalPlayer.valHolder.stamMax.Value
local stamina = game.Players.LocalPlayer.valHolder.stamCurrent.Value
label.Text = ""..stamina.."/"..maxStamina..""

local sprinting = false
local sprintSpeed = 30
local regularSpeed = 15

function startSprint()
	sprinting = true
end

function stopSprint()
	sprinting = false
end

function checkKey(key, isTyping, doSprint)
	local player = game:GetService("Players").LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	if not isTyping then
		if key.KeyCode == sprintKey then
			if doSprint then
				startSprint()
			else
				stopSprint()
				humanoid.WalkSpeed = regularSpeed
				speed.Value = 0.5
			end
		end
	end
end

uis.InputBegan:Connect(function(key, isTyping)
	checkKey(key, isTyping, true)
end)

uis.InputEnded:Connect(function(key, isTyping)
	checkKey(key, isTyping, false)
end)

while wait() do
	if sprinting then
		if stamina > 0 then
			stamina -= 0.1
			humanoid.WalkSpeed = sprintSpeed
			speed.Value = 1
		else
			humanoid.WalkSpeed = regularSpeed
			speed.Value = 0.5
		end
	else
		if stamina < maxStamina then
			stamina += 0.025
		end
		humanoid.WalkSpeed = regularSpeed
	end
	plr.PlayerGui.Interface.bg.Bar:TweenSize(UDim2.new(stamina / maxStamina, 0, -1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0)
	game.Players.LocalPlayer.valHolder.stamCurrent.Value = stamina
end

1 Like

ok I fixed it by adding an else statement in both inputbegan and inputended (2nd script)

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