The Sprint Module: Functions, Settings and GUI doesn’t reset on Character Respawn?
I’m struggling to find an alternative path to Initialize the Module on Character Respawn, without the Module breaking OR doesn’t function.
Module Script:
local Sprint = {}
Sprint.__index = Sprint
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
local SprintGui = script:FindFirstChild("SprintGui")
local StaminaHolder = SprintGui:FindFirstChild("StaminaHolder")
local StaminaBar = StaminaHolder:FindFirstChild("StaminaBar")
local StaminaText = StaminaHolder:FindFirstChild("StaminaText")
local SprintButton = SprintGui:FindFirstChild("SprintButton")
SprintGui.Parent = PlayerGui
local Settings = require(game:GetService("StarterPlayer").StarterPlayerScripts.ClientSettings.C_Settings)
function Sprint:Initialize()
local LastPosition = HRP.Position
function Sprint:UpdateStaminaBar()
local Percentage = Settings.CurrentStamina / Settings.Stamina
StaminaBar.Size = UDim2.new(Percentage, 0, 1, 0)
StaminaText.Text = tostring(math.floor(Settings.CurrentStamina))
end
function Sprint:TweenFOV(targetFOV)
local Tween = TS:Create(Camera, TweenInfo.new(0.2), {FieldOfView = targetFOV})
Tween:Play()
end
function Sprint:FadeIn()
local FadeInBarTween = TS:Create(StaminaBar, TweenInfo.new(Settings.StaminaFadeIn), {BackgroundTransparency = 0.8})
local FadeInTextTween = TS:Create(StaminaText, TweenInfo.new(Settings.StaminaFadeIn), {TextTransparency = 0.4})
FadeInBarTween:Play()
FadeInTextTween:Play()
end
function Sprint:FadeOut()
local FadeOutBarTween = TS:Create(StaminaBar, TweenInfo.new(Settings.StaminaFadeOut), {BackgroundTransparency = 1})
local FadeOutTextTween = TS:Create(StaminaText, TweenInfo.new(Settings.StaminaFadeOut), {TextTransparency = 1})
FadeOutBarTween:Play()
FadeOutTextTween:Play()
end
function Sprint:ShowStaminaBar()
self:FadeIn()
StaminaHolder.Visible = true
end
function Sprint:HideStaminaBar()
self:FadeOut()
StaminaHolder.Visible = false
end
function Sprint:StartSprinting()
if Settings.CurrentStamina <= 0 or not Settings.Moving or Settings.Sprinting or Settings.MovingBackward then return end
Settings.Sprinting = true
Humanoid.WalkSpeed = Settings.SprintSpeed
self:ShowStaminaBar()
self:TweenFOV(Settings.SprintFOV)
end
function Sprint:StopSprinting()
if not Settings.Sprinting then return end
Settings.Sprinting = false
Settings.SprintTime = tick()
Humanoid.WalkSpeed = Settings.WalkSpeed
self:ShowStaminaBar()
self:TweenFOV(Settings.WalkFOV)
end
function Sprint:UpdateBobbles(deltaTime)
local IsMoving = Humanoid.MoveDirection.Magnitude > 0
local IsSprinting = Settings.Sprinting
local Intensity = IsSprinting and Settings.SprintBobble
or (IsMoving and Settings.WalkBobble)
or Settings.IdleBobble
Settings.Bobbing = Settings.Bobbing:Lerp(
CFrame.Angles(
math.rad(math.sin(tick() * (IsSprinting and Settings.FastBobble or IsMoving and Settings.MediumBobble or Settings.SlowBobble)) * Intensity),
0,
math.rad(math.cos(tick() * (IsSprinting and Settings.FastBobble or IsMoving and Settings.MediumBobble or Settings.SlowBobble)) * Intensity)
),
Settings.BobbleRate
)
Camera.CFrame = Camera.CFrame * Settings.Bobbing
end
function Sprint:CheckMovement()
local CurrentPosition = Character.HumanoidRootPart.Position
local MovementDifference = (CurrentPosition - LastPosition).Magnitude
Settings.Moving = MovementDifference >= 0.1 and Humanoid.WalkSpeed
LastPosition = CurrentPosition
end
function Sprint:UpdateMovementDirection()
local moveDirection = Humanoid.MoveDirection
local facingDirection = HRP.CFrame.LookVector
Settings.MovingBackward = moveDirection:Dot(facingDirection) < 0
end
function Sprint:RegenerateStamina()
if not Settings.Sprinting and Settings.CurrentStamina < Settings.Stamina then
if tick() - Settings.SprintTime > Settings.RegenDelay then
Settings.CurrentStamina = math.min(Settings.CurrentStamina + Settings.RegenRate, Settings.Stamina)
self:UpdateStaminaBar()
end
end
end
UIS.InputBegan:Connect(function(Input, GameProcessed)
if not GameProcessed and Input.KeyCode == Enum.KeyCode.LeftShift then
if not Settings.MovingBackward then
Settings.ShiftHeld = true
self:StartSprinting()
print("Sprinting")
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
Settings.ShiftHeld = false
Settings.LastShiftTime = tick()
self:StopSprinting()
print("Walking")
end
end)
RS.RenderStepped:Connect(function(deltaTime)
self:CheckMovement()
self:UpdateMovementDirection()
self:UpdateBobbles(deltaTime)
if Settings.Sprinting and not Settings.MovingBackward then
Settings.CurrentStamina = math.max(Settings.CurrentStamina - Settings.DrainRate, 0)
self:UpdateStaminaBar()
self:FadeIn()
if Settings.CurrentStamina <= 0 then
self:StopSprinting()
end
else
self:RegenerateStamina()
if Settings.MovingBackward then
self:FadeOut()
self:StopSprinting()
self:TweenFOV(Settings.WalkFOV)
end
if Settings.CurrentStamina > Settings.Stamina then
self:FadeOut()
elseif not Settings.ShiftHeld and tick() - Settings.LastShiftTime >= Settings.StaminaThreshold and StaminaHolder.Visible then
self:FadeOut()
end
end
end)
end
return Sprint
Requirement Script:
local SP = game:GetService("StarterPlayer")
local SPS = SP:WaitForChild("StarterPlayerScripts")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ModulesFolder = SPS:WaitForChild("Modules")
if not ModulesFolder then
warn("Modules folder not found in StarterPlayerScripts.")
return
end
local ModulesGUI = ModulesFolder:WaitForChild("GUI's")
local ModulesPlayer = ModulesFolder:WaitForChild("Player")
local Modules = {}
local ModulesInitialized = false
if ModulesFolder then
print("Located: " .. ModulesFolder.Name)
if ModulesGUI then
print("Located GUI Folder: " .. ModulesGUI.Name)
else
warn("GUI folder not found in Modules.")
end
if ModulesPlayer then
print("Located Player Folder: " .. ModulesPlayer.Name)
else
warn("Player folder not found in Modules.")
end
end
local function LoadModules(ModuleFolder)
for _, Module in pairs(ModuleFolder:GetChildren()) do
if Module:IsA("ModuleScript") then
print("Loading Module: " .. Module.Name)
Modules[Module.Name] = require(Module)
if Modules[Module.Name] then
print("Loaded Module: " .. Module.Name .. " successfully.")
else
warn("Failed to load Module: " .. Module.Name)
end
else
warn("Not a ModuleScript: " .. Module.Name)
end
end
end
local function ResetModules()
for ModuleName, Module in pairs(Modules) do
if Module.Reset then
print("Resetting Module: " .. ModuleName) -- Debug
Module.Reset()
else
warn("Module " .. ModuleName .. " does not have a Reset method.")
end
end
end
local function InitializeModules()
if ModulesInitialized then return end
if ModulesGUI then
LoadModules(ModulesGUI)
end
if ModulesPlayer then
LoadModules(ModulesPlayer)
end
local ModulesToInitialize = {
DeviceSupport = Modules.DeviceSupport;
Magnitude = Modules.Magnitude;
Sprint = Modules.Sprint;
Footsteps = Modules.Footsteps;
}
local PlayerModules = {
Sprint = true,
Footsteps = true
}
for ModuleName, Module in pairs(ModulesToInitialize) do
if Module and Module.Initialize then
if PlayerModules[ModuleName] then
print("Initializing Player Module: " .. ModuleName) -- Debug
Module:Initialize(Player)
else
print("Initializing Global Module: " .. ModuleName) -- Debug
Module.Initialize()
end
else
warn("Module " .. ModuleName .. " has no Initialize method.")
end
end
ModulesInitialized = true
end
Player.CharacterAdded:Connect(function(Character)
print("Character added: " .. Character.Name) -- Debug
local Humanoid = Character:WaitForChild("Humanoid")
print("Humanoid found. Resetting and initializing modules...") -- Debug
ModulesInitialized = false
ResetModules()
InitializeModules()
end)
Few questions for some more info. I’m not sure where to start looking without more details. What is the requirement script parented to? At exactly which point does it seem to be failing? Are your prints and warns working? Are there any errors in the output? Which segments of the code seem to work, and which don’t?
There aren’t any Warnings or Errors in the Output. To determine which segment is failing to function, the entire Module isn’t re-initialized after the Character has Respawned.
The Module Script is inside of “StarterPlayerScripts”. I’ve moved the Module Script into “StarterCharacterScripts”, and it still didn’t solve the issue.
I’ve added prints to debug, the print statements do print in the Output, for example:
UIS.InputBegan:Connect(function(Input, GameProcessed)
if not GameProcessed and Input.KeyCode == Enum.KeyCode.LeftShift then
if not Settings.MovingBackward then
Settings.ShiftHeld = true
self:StartSprinting()
print("Sprinting")
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
Settings.ShiftHeld = false
Settings.LastShiftTime = tick()
self:StopSprinting()
print("Walking")
end
end)
Even if the Print statements do occur, it doesn’t define the function for the prints.
So after reading your code, I see a couple issues:
Your script that initializes modules looks for a Reset method in every module. Your running module does not have that method and may cause some things to break by reinitializing it while it’s already active. The fact that you’re getting 2x prints in your GIF proves it’s happening, though.
(note: no. 3 may be causing your module to error and not allow you to sprint).
It’s happened to me that the CharacterAdded event doesn’t immediately fire when your script is within StarterPlayerScripts, (and definitely not when it’s in StarterCharacterScripts because it runs whenever the character is loaded). If this also happens to you, I’d recommend parenting the script to ReplicatedFirst. But if it doesn’t, then don’t bother.
The reason you don’t see your stamina UI after respawning is because your running module doesn’t clone the SprintGui, and thus only gets to parent it once to the player.
I’ve removed the statement: if ModulesInitialized then return end
Nothing was changed when I removed that section of code.
From what I can describe the Module doesn’t essentially break on Character Respawn, however I’ve realised that the Module duplicates somehow. The UserInputService for Shift Held and Shift not Held is being detected and prints when the function is detected, however… the SprintGui doesn’t show? The Character doesn’t Sprint on Shift Held and the Bobble must duplicate because the intensity has doubled from the neutral Bobble states.
Sprint Module:
local Sprint = {}
Sprint.__index = Sprint
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
local SprintGui = script:WaitForChild("SprintGui")
local StaminaHolder = SprintGui:WaitForChild("StaminaHolder")
local StaminaBar = StaminaHolder:WaitForChild("StaminaBar")
local StaminaText = StaminaHolder:WaitForChild("StaminaText")
local SprintButton = SprintGui:WaitForChild("SprintButton")
SprintGui.Parent = PlayerGui
local Settings = require(game:GetService("StarterPlayer").StarterPlayerScripts.ClientSettings.C_Settings)
function Sprint:Initialize()
local LastPosition = HRP.Position
function self:UpdateStaminaBar()
local Percentage = Settings.CurrentStamina / Settings.Stamina
StaminaBar.Size = UDim2.new(Percentage, 0, 1, 0)
StaminaText.Text = tostring(math.floor(Settings.CurrentStamina))
end
function self:TweenFOV(targetFOV)
local Tween = TS:Create(Camera, TweenInfo.new(0.2), {FieldOfView = targetFOV})
Tween:Play()
end
function self:FadeIn()
local FadeInBarTween = TS:Create(StaminaBar, TweenInfo.new(Settings.StaminaFadeIn), {BackgroundTransparency = 0.8})
local FadeInTextTween = TS:Create(StaminaText, TweenInfo.new(Settings.StaminaFadeIn), {TextTransparency = 0.4})
FadeInBarTween:Play()
FadeInTextTween:Play()
end
function self:FadeOut()
local FadeOutBarTween = TS:Create(StaminaBar, TweenInfo.new(Settings.StaminaFadeOut), {BackgroundTransparency = 1})
local FadeOutTextTween = TS:Create(StaminaText, TweenInfo.new(Settings.StaminaFadeOut), {TextTransparency = 1})
FadeOutBarTween:Play()
FadeOutTextTween:Play()
end
function self:ShowStaminaBar()
self:FadeIn()
StaminaHolder.Visible = true
end
function self:HideStaminaBar()
self:FadeOut()
StaminaHolder.Visible = false
end
function self:StartSprinting()
if Settings.CurrentStamina <= 0 or not Settings.Moving or Settings.Sprinting or Settings.MovingBackward then return end
Settings.Sprinting = true
Humanoid.WalkSpeed = Settings.SprintSpeed
self:ShowStaminaBar()
self:TweenFOV(Settings.SprintFOV)
end
function self:StopSprinting()
if not Settings.Sprinting then return end
Settings.Sprinting = false
Settings.SprintTime = tick()
Humanoid.WalkSpeed = Settings.WalkSpeed
self:ShowStaminaBar()
self:TweenFOV(Settings.WalkFOV)
end
function self:UpdateBobbles(deltaTime)
local IsMoving = Humanoid.MoveDirection.Magnitude > 0
local IsSprinting = Settings.Sprinting
local Intensity = IsSprinting and Settings.SprintBobble
or (IsMoving and Settings.WalkBobble)
or Settings.IdleBobble
Settings.Bobbing = Settings.Bobbing:Lerp(
CFrame.Angles(
math.rad(math.sin(tick()
* (IsSprinting
and Settings.FastBobble
or IsMoving
and Settings.MediumBobble
or Settings.SlowBobble))
* Intensity),
0,
math.rad(math.cos(tick()
* (IsSprinting
and Settings.FastBobble
or IsMoving
and Settings.MediumBobble
or Settings.SlowBobble))
* Intensity)
),
Settings.BobbleRate
)
Camera.CFrame = Camera.CFrame * Settings.Bobbing
end
function self:CheckMovement()
local CurrentPosition = HRP.Position
local MovementDifference = (CurrentPosition - LastPosition).Magnitude
Settings.Moving = MovementDifference >= 0.1 and Humanoid.WalkSpeed
LastPosition = CurrentPosition
end
function self:UpdateMovementDirection()
local moveDirection = Humanoid.MoveDirection
local facingDirection = HRP.CFrame.LookVector
Settings.MovingBackward = moveDirection:Dot(facingDirection) < 0
end
function self:RegenerateStamina()
if not Settings.Sprinting and Settings.CurrentStamina < Settings.Stamina then
if tick() - Settings.SprintTime > Settings.RegenDelay then
Settings.CurrentStamina = math.min(Settings.CurrentStamina + Settings.RegenRate, Settings.Stamina)
self:UpdateStaminaBar()
end
end
end
UIS.InputBegan:Connect(function(Input, GameProcessed)
if not GameProcessed and Input.KeyCode == Enum.KeyCode.LeftShift then
if not Settings.MovingBackward then
Settings.ShiftHeld = true
self:StartSprinting()
print("Sprinting") -- Debug (If the Character has Respawned this prints duplicates.)
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.LeftShift then
Settings.ShiftHeld = false
Settings.LastShiftTime = tick()
self:StopSprinting()
print("Walking") -- Debug (If the Character has Respawned this prints duplicates.)
end
end)
RS.RenderStepped:Connect(function(deltaTime)
self:CheckMovement()
self:UpdateMovementDirection()
self:UpdateBobbles(deltaTime)
if Settings.Sprinting and not Settings.MovingBackward then
Settings.CurrentStamina = math.max(Settings.CurrentStamina - Settings.DrainRate, 0)
self:UpdateStaminaBar()
self:FadeIn()
if Settings.CurrentStamina <= 0 then
self:StopSprinting()
end
else
self:RegenerateStamina()
if Settings.MovingBackward then
self:FadeOut()
self:StopSprinting()
self:TweenFOV(Settings.WalkFOV)
end
if Settings.CurrentStamina > Settings.Stamina then
self:FadeOut()
elseif not Settings.ShiftHeld and tick() - Settings.LastShiftTime >= Settings.StaminaThreshold and StaminaHolder.Visible then
self:FadeOut()
end
end
end)
end
return Sprint
Requirement Script:
local SP = game:GetService("StarterPlayer")
local SPS = SP:WaitForChild("StarterPlayerScripts")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ModulesFolder = SPS:WaitForChild("Modules")
if not ModulesFolder then
warn("Modules folder not found in StarterPlayerScripts.")
return
end
local ModulesGUI = ModulesFolder:WaitForChild("GUI's")
local ModulesPlayer = ModulesFolder:WaitForChild("Player")
local Modules = {}
local ModulesInitialized = false
if ModulesFolder then
print("Located: " .. ModulesFolder.Name) -- Debug
if ModulesGUI then
print("Located GUI Folder: " .. ModulesGUI.Name) -- Debug
else
warn("GUI folder not found in Modules.")
end
if ModulesPlayer then
print("Located Player Folder: " .. ModulesPlayer.Name) -- Debug
else
warn("Player folder not found in Modules.")
end
end
local function LoadModules(ModuleFolder)
for _, Module in pairs(ModuleFolder:GetChildren()) do
if Module:IsA("ModuleScript") then
print("Loading Module: " .. Module.Name)
Modules[Module.Name] = require(Module)
if Modules[Module.Name] then
print("Loaded Module: " .. Module.Name .. " successfully.") -- Debug
else
warn("Failed to load Module: " .. Module.Name)
end
else
warn("Not a ModuleScript: " .. Module.Name)
end
end
end
--[[local function ResetModules()
for ModuleName, Module in pairs(Modules) do
if Module.Reset then
print("Resetting Module: " .. ModuleName) -- Debug
Module.Reset()
else
warn("Module " .. ModuleName .. " does not have a Reset method.")
end
end
end]]
local function InitializeModules()
--if ModulesInitialized then return end
if ModulesGUI then
LoadModules(ModulesGUI)
end
if ModulesPlayer then
LoadModules(ModulesPlayer)
end
local ModulesToInitialize = {
DeviceSupport = Modules.DeviceSupport;
Magnitude = Modules.Magnitude;
Sprint = Modules.Sprint;
Footsteps = Modules.Footsteps;
}
local PlayerModules = {
Sprint = true;
Footsteps = true;
}
for ModuleName, Module in pairs(ModulesToInitialize) do
if Module and Module.Initialize then
if PlayerModules[ModuleName] then
print("Initializing Player Module: " .. ModuleName) -- Debug
Module:Initialize(Player)
else
print("Initializing Global Module: " .. ModuleName) -- Debug
Module.Initialize()
end
else
warn("Module " .. ModuleName .. " has no Initialize method.")
end
end
ModulesInitialized = true
end
Player.CharacterAdded:Connect(function(Character)
print("Character added: " .. Character.Name) -- Debug
local Humanoid = Character:WaitForChild("Humanoid")
print("Humanoid found. Resetting and initializing modules...") -- Debug
ModulesInitialized = false
--ResetModules()
InitializeModules()
end)
additionally, events from UserInputService will remain active because the running module never gets to reset.
I kind of described the issues you’re mentioning once again and are not fixed in your current code. If you didn’t understand my previous message, I have no problem describing it to you in further detail so you can understand.
@tinctoriall
I have a similar set-up for another Module Script based on Footstep initialization. The Module Script actually resets upon Character Respawn, trying to attract the same functionality to the Sprint Module.
Footstep Module:
local Footsteps = {}
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Settings = require(game:GetService("StarterPlayer").StarterPlayerScripts.ClientSettings.C_Settings)
function Footsteps.Initialize()
local function SetupCharacter(Character)
local HRP = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Running:Connect(function(Speed)
Footsteps.Running(Speed, Humanoid, HRP)
end)
end
if Player.Character then
SetupCharacter(Player.Character)
end
Player.CharacterAdded:Connect(function(Character)
SetupCharacter(Character)
end)
end
local FootstepMaterials = {
[Enum.Material.Grass] = {
SoundId = "rbxassetid://9083837523",
Volume = Settings.Volume,
PlaybackSpeed = Settings.Speed,
RollOffMaxDistance = Settings.MaxDistance,
RollOffMinDistance = Settings.MinDistance,
Looped = Settings.Looped,
},
[Enum.Material.Ground] = {
SoundId = "rbxassetid://9083852561",
Volume = Settings.Volume,
PlaybackSpeed = Settings.Speed,
RollOffMaxDistance = Settings.MaxDistance,
RollOffMinDistance = Settings.MinDistance,
Looped = Settings.Looped,
},
[Enum.Material.Fabric] = {
SoundId = "rbxassetid://133705377",
Volume = Settings.Volume,
PlaybackSpeed = Settings.Speed,
RollOffMaxDistance = Settings.MaxDistance,
RollOffMinDistance = Settings.MinDistance,
Looped = Settings.Looped,
},
[Enum.Material.Sand] = {
SoundId = "rbxassetid://212011266",
Volume = Settings.Volume,
PlaybackSpeed = Settings.Speed,
RollOffMaxDistance = Settings.MaxDistance,
RollOffMinDistance = Settings.MinDistance,
Looped = Settings.Looped,
},
}
local Manager = {}
Manager.Footstep = nil
function Manager:CreateFootstep(HRP)
if not self.Footstep then
self.Footstep = Instance.new("Sound")
self.Footstep.Parent = HRP
self.Footstep.Name = "Footstep"
end
return self.Footstep
end
function Manager:PlayFootstep(Material, Speed, HRP)
local Footstep = self:CreateFootstep(HRP)
local Properties = FootstepMaterials[Material]
if Properties then
Footstep.SoundId = Properties.SoundId
Footstep.Volume = Properties.Volume
Footstep.PlaybackSpeed = Properties.PlaybackSpeed * (Speed / 6)
Footstep.Looped = Properties.Looped
Footstep.RollOffMaxDistance = Properties.RollOffMaxDistance
Footstep.RollOffMinDistance = Properties.RollOffMinDistance
Footstep.RollOffMode = Enum.RollOffMode.Linear
if not Footstep.IsPlaying then
Footstep:Play()
end
end
end
function Manager:StopFootstep()
if self.Footstep then
self.Footstep:Stop()
self.Footstep:Destroy()
self.Footstep = nil
end
end
function Footsteps.Running(Speed, Humanoid, HRP)
if Speed > 1 then
local Material = Humanoid.FloorMaterial
if FootstepMaterials[Material] then
Manager:PlayFootstep(Material, Speed, HRP)
end
else
Manager:StopFootstep()
end
end
return Footsteps
I know this is probably the dumbest reply here, but I had this same issue. If the script that requires the module is deleted, the module will stop functioning.
The Module Script isn’t deleted because the Module Script still prints on the Sprint and Non-sprint detection function, however the function doesn’t input properly.