The purpose of this script is for handling a sprinting effect. Everytime you sprint, the Run.Heartbeat event from the server script will deduct your max stamina with the stamina cost until it reaches less than the stamina cost. My problem is the first time you tried to sprint whole holding shift, it ONLY subtracts once and it stopped subtracting. And whenever the stamina value is changed, it will fire a remote event. And that also doesn’t work. I need desperate help to solve both of this issue. Please help.
Client script:
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
-- instances
local RemotesFolder = ReplicatedStorage:WaitForChild("Remotes")
local SprintingSignal = RemotesFolder:WaitForChild("SprintingSignal")
local StaminaUpdate = RemotesFolder:WaitForChild("StaminaUpdate")
-- setup
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
SprintingSignal:FireServer("Began")
end
end)
UserInputService.InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift then
SprintingSignal:FireServer("Ended")
end
end)
StaminaUpdate.OnClientEvent:Connect(function(currentStamina,maxStamina)
print("changed")
local size = (currentStamina / maxStamina) * 0.27
script.Parent.Bar.Size = UDim2.new(size,0,0.09,0)
end)
Server script:
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Run = game:GetService("RunService")
local Players = game:GetService("Players")
-- spriting settings
local walkspeedMultiplier = 1.6
local maxStamina = 100
local regen = 2 -- per frame
local decrease = 10 -- per frame
local resetSpeed = 16
local sprintingPlayers = {}
-- instances
local RemotesFolder = ReplicatedStorage:WaitForChild("Remotes")
local SprintingSignal = RemotesFolder:WaitForChild("SprintingSignal")
local StaminaUpdate = RemotesFolder:WaitForChild("StaminaUpdate")
-- setup
-- to handle the size of the gui
Players.PlayerAdded:Connect(function(player)
local stamina = Instance.new("IntValue")
stamina.Parent = player
stamina.Name = "Stamina"
stamina.Value = maxStamina
stamina:GetPropertyChangedSignal("Value"):Connect(function() -- this is supposed to be fired whenever the value is changed, unfortunately it didn't got fired even though the value is changed.
print("hello")
StaminaUpdate:FireClient(player,stamina.Value,maxStamina)
end)
print("event connected")
end)
-- handles the sprinting
SprintingSignal.OnServerEvent:Connect(function(player,state)
-- define characters and stuffs
local char = player.Character
local humanoid = char.Humanoid
if state == "Began" then
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
sprintingPlayers[player.UserId] = humanoid.WalkSpeed or 16 -- assign their current speed before multiplying
humanoid.WalkSpeed *= walkspeedMultiplier
elseif state == "Ended" then
humanoid.WalkSpeed = sprintingPlayers[player.UserId] -- gives back the speed they had before their speed gets multiplied
sprintingPlayers[player.UserId] = nil -- discard the value
end
end)
-- handles the size for gui
Run.Heartbeat:Connect(function()
for _, player in pairs(Players:GetPlayers()) do
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local userId = player.UserId
local stamina = player.Stamina.Value
if not sprintingPlayers[userId] then -- if selected player is not sprinting, do the regen
if stamina > maxStamina then -- just incase if player is hacking or speed is over max
stamina = maxStamina
elseif stamina < maxStamina then -- the player's stamina needs to be regen
stamina += regen
end
elseif sprintingPlayers[userId] then -- if selected player is sprinting
print("player is sprinting")
if stamina >= decrease then -- if their current stamina is more than or equal the cost
stamina -= decrease -- this is the code line that it only subtracts once.
print(stamina)
else -- if their speed is less tahn the cost
humanoid.WalkSpeed = sprintingPlayers[player.UserId] -- gives back the speed they had before their speed gets multiplied
sprintingPlayers[player.UserId] = nil -- discard the value
end
end
end
end)
GUI Hierachy (this bar will change according to the stamina you have.)


PLEASE HELP ME.