Sprinting not working after reset

I made this sprint script by following a tutorial and slightly altered it to display animations when running. The script works normally upon entering the game, however, after the player dies/resets, it stops working (no errors show up on the console). Its a local script in StarterPlayerScripts. Any solutions?


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

local sprintSpeed = 70
local walkSpeed = 16

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")




local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9580521133"
local animplay = animator:LoadAnimation(anim)




local function beginSprint(input, gameProcessed)
	if not gameProcessed then
		if player.Character.Attacking.Value == 0 then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.LeftControl then
				humanoid.WalkSpeed = 32
					
					
				
					
					
				animplay:Play()
			end
		end
	end
	end
	end

local function endSprint(input, gameProcessed)
	if not gameProcessed then
		if player.Character.Attacking.Value == 0 then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.LeftControl then
					humanoid.WalkSpeed = 16
					
				
					
					
	animplay:Stop()
				
			end
		end
	end
	end	
	end




userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)

When a Player dies, their Character gets destroyed and recreated. In your code you are not accounting for that. You must add an event for when the Player dies and a new character is spawned.

ex.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
Player.CharacterAdded:Connect(function(NewCharacter: Model)
    Character = NewCharacter;
end) 

I would recommend changing the script to a regular script in starter character scripts because when a player dies there is no indication to the local script in the player but if it is in the character than a new instance of the script is created each time which starts the script again therefore making it work again

This works too
(Character Limit)

How would i accomplish that? is there a function for it?

But you cant get UserInputService in a normal script, right?

1 Like

Excuse my incompetence, but the script doesnt change anything, any idea why that is?

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

local sprintSpeed = 32
local walkSpeed = 16

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9580521133"
local animplay = animator:LoadAnimation(anim)

player.CharacterAdded:Connect(function(NewCharacter)
	character = NewCharacter
	humanoid = character:WaitForChild("Humanoid")
	animator = humanoid:WaitForChild("Animator")
	
	anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://9580521133"
	animplay = animator:LoadAnimation(anim)
end) 

local function beginSprint(input, gameProcessed)
	if not gameProcessed then
		if player.Character.Attacking.Value == 0 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftControl then
					humanoid.WalkSpeed = sprintSpeed
					animplay:Play()
				end
			end
		end
	end
end

local function endSprint(input, gameProcessed)
	if not gameProcessed then
		if player.Character.Attacking.Value == 0 then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftControl then
					humanoid.WalkSpeed = walkSpeed
					animplay:Stop()
				end
			end
		end
	end	
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)
2 Likes