Humanoid.MoveDirection.Magnitude is 0 after death

I want my player to be able to run even after he dies.

After you die, the script doesn’t work anymore. There is no error in the output and I can’t really explain why it doesn’t work. I checked which if statement stops the script and found that Humanoid.MoveDirection.Magnitude is always 0 after I die.

I’ve searched for solutions but haven’t found anything that would help. I also added a function that redefines Humanoid after I die, thinking it might fix the problem. But it didn’t.

-- 
local NormalWalkSpeed = 15
local SprintSpeed = 40
local CameraEffect = true
local stamina = 200
local isRunning = false
local StaminaFrame = script.Parent

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

local Camera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key, gameProcessedEvent)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
	end
end)


UIS.InputEnded:Connect(function(key, gameProcessedEvent)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if stamina > 0 then
		if isRunning then
			if Humanoid.MoveDirection.Magnitude > 0 then
				--print("Magnutide")
				Humanoid.WalkSpeed = SprintSpeed
				stamina -= 0.5
				StaminaFrame.Size = StaminaFrame.Size - UDim2.new(0.0006875,0,0,0)
			end
		else
			Humanoid.WalkSpeed = NormalWalkSpeed
			wait(2)
		end
	else
		Humanoid.WalkSpeed = NormalWalkSpeed
		wait(2)
	end
	
	if stamina < 200 then
		if not isRunning then
			stamina += 0.5
			StaminaFrame.Size = StaminaFrame.Size + UDim2.new(0.0006875,0,0,0)
		end
	end
end)

Credit for the script: How to make a Sprint and Stamina System - Roblox Scripting Tutorial - YouTube
Most of this script is copy pasted from the script in this tutorial!

1 Like

Easiest fix would be to use your model your own MoveDirection instead of having to rely on the property off the Humanoid, as I believe on death it’s always hard-set to (0, 0, 0)

1 Like

At first I had the local script in StarterGUI to change the frame that shows the stamina. I have decided to put it in StarterCharacterScripts now, as it fixes the problem. I will then handle the frame with a second local script.

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