Can I know why my code doesn't work after I reset my r

My code works fine the first try but after I reset my character it breaks

local PlrService = game:GetService("Players")
local Uis = game:GetService("UserInputService")
local Ts = game:GetService("TweenService")
local RS = game:GetService("RunService")
local Ti = TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0)

local Plr = PlrService.LocalPlayer
local Character =  Plr.CharacterAdded:Wait() or Plr.Character
local Humanoid = Character:WaitForChild("Humanoid")



local Cam = workspace.CurrentCamera
local Anim = script:WaitForChild("SprintAnim")
local AnimTrack = Humanoid:LoadAnimation(Anim)


local IsSprinting = false
local Moving = false
local SprintSpeed = 30
local WalkSpeed = 12
local SprintFOV = 85


local WalkFOV = Cam.FieldOfView
local Rs = game.ReplicatedStorage
local PlayerGui = Plr.PlayerGui
local Stamina = PlayerGui:WaitForChild("PlayerInterfaceGUI").StaminaBackground.StaminaBar
local Tick  = math.huge


local Toggle = false
local Toggle2 = false


local function Sprint(TOGGLE,ISSPRINTING,FOV,WS)
	Toggle = TOGGLE

	IsSprinting = ISSPRINTING
	Ts:Create(Cam,Ti,{FieldOfView = FOV}):Play()
	Humanoid.WalkSpeed = WS
	
	
end


Cam.Changed:Connect(function(prop)
	if prop == "FieldOfView" then
		if IsSprinting == true then return end
		SprintFOV = Cam.FieldOfView + 15
		WalkFOV = 70
	end
end)

Rs:WaitForChild("Remotes"):WaitForChild("PowerUp").OnClientEvent:Connect(function(PowerUp)
	if PowerUp == "SpeedBoostPowerup" then
		SprintSpeed = 60
		WalkSpeed = 24
		wait(5)
		SprintSpeed = 30
		WalkSpeed = 12

	elseif PowerUp == "JumpBoostPowerup" then
		SprintSpeed = 60
		WalkSpeed = 24
		Humanoid.JumpPower = 100
		wait(5)
		SprintSpeed = 30
		WalkSpeed = 12
		Humanoid.JumpPower = 50

	end
	
	if IsSprinting == true then
		Sprint(true,true,SprintFOV,SprintSpeed)
	elseif IsSprinting == false then
		Sprint(false,false,WalkFOV,WalkSpeed)
	end
	wait(5.1)
	
	if IsSprinting == true then
		Sprint(true,true,SprintFOV,SprintSpeed)
	elseif IsSprinting == false then
		Sprint(false,false,WalkFOV,WalkSpeed)
	end
end)

Uis.InputBegan:Connect(function(Input, GameProcessed)
	if GameProcessed then return end
	if Moving == false then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		
		if Toggle == false and Plr:GetAttribute("Stamina") ~= 0 then
			Sprint(true,true,SprintFOV,SprintSpeed)
			AnimTrack:Play()
			
		elseif Toggle == true then
			Sprint(false,false,WalkFOV,WalkSpeed)
			AnimTrack:Stop()
			
		end
	end
end)
Humanoid.Changed:Connect(function(prop)
	if prop == "MoveDirection" then
		if Humanoid.MoveDirection == Vector3.new(0,0,0) then
			Moving = false
			Sprint(false,false,WalkFOV,WalkSpeed)
			AnimTrack:Stop()
		end
	end
end)

Humanoid.Changed:Connect(function(prop)
	if prop == "MoveDirection" then
		if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
			Moving = true
		end
	end
end)

RS.Heartbeat:Connect(function()

	
	if Toggle == true and Moving == true then
		Plr:SetAttribute("Stamina",Plr:GetAttribute("Stamina") - Plr:GetAttribute("DecreaseSprintSpeed")) 
		Tick = os.time()
	else
		if os.time()-Tick >= 3 then
				Plr:SetAttribute("Stamina",Plr:GetAttribute("Stamina") + Plr:GetAttribute("RegenWalkSpeed"))
				if Plr:GetAttribute("Stamina") >= 99.5 then
					Plr:SetAttribute("Stamina",100)
				end
			end
	end
	
	if Plr:GetAttribute("Stamina") == 0 then
		Sprint(false,false,WalkFOV,WalkSpeed)
		AnimTrack:Stop()
	end
	
	
	
end)

Honestly not sure what the problem is overall (any errors when you reset? Where is this script located?). I will note one potentially problematic thing is what I quoted. Since you will wait for CharacterAdded to fire before you check if they exist. You should probably switch the order of those conditions.

You should update the Character and Humanoid References:
Use the CharacterAdded event to reinitialize references when the character is reset.

then reapply the animations:
Reload animations when the Humanoid changes.

lastly, reconnect the listeners:
Disconnect and reconnect all listeners dependent on Character or Humanoid.

local PlrService = game:GetService("Players")
local Uis = game:GetService("UserInputService")
local Ts = game:GetService("TweenService")
local RS = game:GetService("RunService")
local Ti = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)

local Plr = PlrService.LocalPlayer
local Cam = workspace.CurrentCamera

local SprintFOV = 85
local WalkFOV = Cam.FieldOfView
local SprintSpeed = 30
local WalkSpeed = 12

local IsSprinting = false
local Moving = false
local Toggle = false
local Anim
local AnimTrack
local Humanoid

-- Update references when the character resets
local function onCharacterAdded(character)
    Humanoid = character:WaitForChild("Humanoid")
    Anim = script:WaitForChild("SprintAnim")
    AnimTrack = Humanoid:LoadAnimation(Anim)

    -- Reconnect listeners
    Humanoid.Changed:Connect(function(prop)
        if prop == "MoveDirection" then
            if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
                Moving = false
                if IsSprinting then
                    Sprint(false, false, WalkFOV, WalkSpeed)
                end
                AnimTrack:Stop()
            else
                Moving = true
            end
        end
    end)
end

-- Handle the current character
Plr.CharacterAdded:Connect(onCharacterAdded)
if Plr.Character then
    onCharacterAdded(Plr.Character)
end

local function Sprint(TOGGLE, IS_SPRINTING, FOV, WS)
    Toggle = TOGGLE
    IsSprinting = IS_SPRINTING
    Ts:Create(Cam, Ti, { FieldOfView = FOV }):Play()
    Humanoid.WalkSpeed = WS
end

Uis.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed or not Moving then return end
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if not Toggle and Plr:GetAttribute("Stamina") > 0 then
            Sprint(true, true, SprintFOV, SprintSpeed)
            AnimTrack:Play()
        else
            Sprint(false, false, WalkFOV, WalkSpeed)
            AnimTrack:Stop()
        end
    end
end)

RS.Heartbeat:Connect(function()
    if Toggle and Moving then
        Plr:SetAttribute("Stamina", Plr:GetAttribute("Stamina") - Plr:GetAttribute("DecreaseSprintSpeed"))
    else
        if os.time() - Tick >= 3 then
            local regen = Plr:GetAttribute("Stamina") + Plr:GetAttribute("RegenWalkSpeed")
            Plr:SetAttribute("Stamina", math.min(100, regen))
        end
    end

    if Plr:GetAttribute("Stamina") <= 0 then
        Sprint(false, false, WalkFOV, WalkSpeed)
        AnimTrack:Stop()
    end
end)

Random question if I had other scripts that reference the character model would I need to do something like this all the time

I will try your suggestion later and also the script is located in starter player

No, you can simply put it in a module like this one.
and simply require it on the script you are trying to use it–
and YES because you need to reference the newly spawned character for it to work,

-- CharacterManager Module
local Players = game:GetService("Players")

local CharacterManager = {}
CharacterManager.CurrentCharacter = nil
CharacterManager.CurrentHumanoid = nil

local player = Players.LocalPlayer

local function onCharacterAdded(character)
    CharacterManager.CurrentCharacter = character
    CharacterManager.CurrentHumanoid = character:WaitForChild("Humanoid")

    -- Notify other scripts if needed
    if CharacterManager.OnCharacterReset then
        CharacterManager.OnCharacterReset(character)
    end
end

player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
    onCharacterAdded(player.Character)
end

return CharacterManager

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