So I have module script to manage my stamina. A roll/dash, sprint and double jump script. Each of them subtract some value from a stamina bar. Now when I start and use roll or double jump it subtracts stamina from the bar but it doesnt tween the bar or change the text. It does only after I use sprint. After that it works normally until the bar reaches 100 then the same thing happens again.
I tried to ask chat GPT and that didnt help
I’m pretty sure something is wrong in the module script
1 Like
This is correct. You do not want to tween what’s in starter gui.
1 Like
I suggest you add a player value to your modulescript function, updateStamina,
it should look something like this
local function updateStamina(value, player : Player)
instead of
local function updateStamina(value)
You can also change this line,
local StaminaBar = game.StarterGui["RectangleGUI's"].StaminaBarGUI.StaminaBar
to
local StaminaBar = player.PlayerGui:WaitForChild("RectangleGUI's").StaminaBarGUI.StaminaBar
1 Like
ReplicatedStorage.StaminaManager:9: attempt to index nil with ‘PlayerGui’
1 Like
Can you send over the entire script? I’ll edit it from there.
1 Like
Sure you want the module script?
1 Like
I need StaminaManager, the one that errored.
local module = {}
local playerStamina = 100
local maxStamina = 100
local function updateStamina(value, player : Player)
playerStamina = math.clamp(playerStamina + value, 0, maxStamina)
local StaminaBar = player.PlayerGui:WaitForChild("RectangleGUI's").StaminaBarGUI.StaminaBar
StaminaBar.StaminaStatus.Text = playerStamina .. " / " .. maxStamina
StaminaBar.OrangeFrame:TweenSize(
UDim2.new(playerStamina / maxStamina, 0, 1, 0),
"Out",
"Linear",
0
)
wait(0.1)
end
local function getStamina()
return playerStamina
end
local function getMaxStamina()
return maxStamina
end
return {
updateStamina = updateStamina,
getStamina = getStamina,
getMaxStamina = getMaxStamina
}
That code is fine, can you send the script that calls updateStamina?
local tweenservice = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local cam = game.Workspace.Camera
local char = Player.Character:WaitForChild("Humanoid")
local staminabar = script.Parent:WaitForChild("StaminaBar")
local StaminaStatus = staminabar.StaminaStatus
local running = false
local StaminaManager = require(game.ReplicatedStorage.StaminaManager) -- Adjust the path to the StaminaManager script
local sprintDepletionAmount = 1 -- Adjust this value as needed (how much stamina is consumed while sprinting)
local maxStamina = StaminaManager.getMaxStamina()
local function onSprintBegan()
running = true
char.WalkSpeed = 20
local fovTween = tweenservice:Create(
cam,
TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
{FieldOfView = 75}
)
fovTween:Play()
while StaminaManager.getStamina() > 0 and running and Player.Character.Humanoid.MoveDirection.Magnitude > 0 do
StaminaManager.updateStamina(-sprintDepletionAmount)
local stamina = StaminaManager.getStamina()
StaminaStatus.Text = stamina .. " / " .. maxStamina
staminabar.OrangeFrame:TweenSize(
UDim2.new(stamina / maxStamina, 0, 1, 0),
"Out",
"Linear",
0
)
wait(0.1)
end
char.WalkSpeed = 14
local fovTween2 = tweenservice:Create(
cam,
TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
{FieldOfView = 70}
)
fovTween2:Play()
end
local function onSprintEnded()
-- Your sprint ended code here (e.g., stop the sprint animation, reset player speed, etc.)
running = false
while StaminaManager.getStamina() < maxStamina and not running do
StaminaManager.updateStamina(1)
local stamina = StaminaManager.getStamina()
StaminaStatus.Text = stamina .. " / " .. maxStamina
staminabar.OrangeFrame:TweenSize(
UDim2.new(stamina / maxStamina, 0, 1, 0),
"Out",
"Linear",
0
)
wait(0.05)
end
end
-- Connect the functions to the input events
uis.InputBegan:Connect(function(input, isProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
onSprintBegan()
end
end)
uis.InputEnded:Connect(function(input, isProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
onSprintEnded()
end
end)
There’s 3 scripts that use it .
change
while StaminaManager.getStamina() > 0 and running and Player.Character.Humanoid.MoveDirection.Magnitude > 0 do
StaminaManager.updateStamina(-sprintDepletionAmount)
local stamina = StaminaManager.getStamina()
StaminaStatus.Text = stamina .. " / " .. maxStamina
staminabar.OrangeFrame:TweenSize(
UDim2.new(stamina / maxStamina, 0, 1, 0),
"Out",
"Linear",
0
)
wait(0.1)
end
to
while StaminaManager.getStamina() > 0 and running and Player.Character.Humanoid.MoveDirection.Magnitude > 0 do
StaminaManager.updateStamina(-sprintDepletionAmount, game.Players.LocalPlayer)
local stamina = StaminaManager.getStamina()
StaminaStatus.Text = stamina .. " / " .. maxStamina
staminabar.OrangeFrame:TweenSize(
UDim2.new(stamina / maxStamina, 0, 1, 0),
"Out",
"Linear",
0
)
wait(0.1)
end
that did exactly nothing . .- .
You still get the error? You need to be sure to include, wherever the function updateStamina is called, that you provide the player as an argument, along with the value, hence (value, player)
I didnt use cuz it gave error
local StaminaBar = player.PlayerGui:WaitForChild("RectangleGUI's").StaminaBarGUI.StaminaBar
That code is correct, as I have stated, and you just need to find wherever the function is called, and add the player argument to it.
It would look something like this
local StaminaManager = require(path.to.script)
StaminaManager.updateStamina([replace this with your value, or the code to get it], game.Players.LocalPlayer)
I did that but it didnt change anything
I can give you the script for double jump and roll if you need
Then I can’t help, as that’s the only problem I saw.