I've made an FE compatible sprinting GUI that depletes when the user holds LeftShift. When the sprinting RE is fired to the server, the player's speed is changed to 24 when sprinting, and back to 16 otherwise.
Now, there are two problems that I'm having here with this script:
1) For some reason the bar depletes faster in-game rather than in test mode. I've come up with a work around by increasing the time it takes to deplete, but I'm still not sure why this happens.
2) The stamina bar always moves up to 101 stamina rather than the preferred 100 even when I've made checks to see if the bar should replete only when currentStamina < maxStamina. Note that this occurs both in-game and in test mode.
CLIENT:
local players = game:GetService('Players') local replicatedStorage = game:GetService('ReplicatedStorage') local userInputService = game:GetService('UserInputService') local player = players.LocalPlayer local character = player.Character or player.CharacterAdded:wait() local humanoid = character:WaitForChild('Humanoid') local playerGui = player:WaitForChild('PlayerGui') local gui = playerGui:WaitForChild('GUI') local events = replicatedStorage:WaitForChild('Events') local staminaGui = gui:WaitForChild('Stamina') local staminaBar = staminaGui:WaitForChild('Frame'):WaitForChild('Frame') local staminaEvent = events:WaitForChild('Stamina_Event') local currStamina = 100 function updateStamina() staminaBar:TweenSize(UDim2.new(currStamina / 100, 0, 0.5, 0), 'Out', 'Quad', 0.1) end userInputService.InputBegan:Connect(function(input, event) if event or currStamina <= 0 then return end if input.KeyCode.Name == 'LeftShift' then staminaEvent:FireServer('sprint', 24) repeat if humanoid.MoveDirection.Magnitude > 0.1 then currStamina = currStamina - 1 updateStamina() end wait() until not userInputService:IsKeyDown(Enum.KeyCode.LeftShift) or currStamina <= 0 staminaEvent:FireServer('sprint', 16) if currStamina <= 0 then wait(2) else wait(1) end while not userInputService:IsKeyDown(Enum.KeyCode.LeftShift) and currStamina < 100 do currStamina = currStamina + 1 updateStamina() wait(0.4) end end end)
SERVER:
events:WaitForChild('Stamina_Event').OnServerEvent:Connect(function(player, request, data) if request == 'sprint' then player.Character.Humanoid.WalkSpeed = data end end)
To anyone else who may be wondering how to fix an issue such as this, I have solved my problem by deciding to make the GUI with Offset
instead of Scale
. I believe that Roblox is having issues scaling the size of the GUI with what the actual stam bar supposed to tween to. Now that I think of it, it probably wasn't a good idea to use Scale
and an AspectRatioConstraint
because of how thin the bar gets with smaller screens than my own.
This is the best I can do tell me if it works!
local players = game:GetService('Players') local replicatedStorage = game:GetService('ReplicatedStorage') local userInputService = game:GetService('UserInputService') local player = players.LocalPlayer local character = player.Character or player.CharacterAdded:wait() local humanoid = character:WaitForChild('Humanoid') local playerGui = player:WaitForChild('PlayerGui') local gui = playerGui:WaitForChild('GUI') local events = replicatedStorage:WaitForChild('Events') local staminaGui = gui:WaitForChild('Stamina') local staminaBar = staminaGui:WaitForChild('Frame'):WaitForChild('Frame') local staminaEvent = events:WaitForChild('Stamina_Event') local currStamina = 100 function updateStamina() staminaBar:TweenSize(UDim2.new(currStamina / 100, 0, 0.5, 0), 'Out', 'Quad', 0.1) end userInputService.InputBegan:Connect(function(input, event) if event or currStamina <= 0 then return end if input.KeyCode.Name == 'LeftShift' then staminaEvent:FireServer('sprint', 24) repeat if humanoid.MoveDirection.Magnitude > 0.1 then currStamina = currStamina - 1 updateStamina() end wait() until not userInputService:IsKeyDown(Enum.KeyCode.LeftShift) or currStamina <= 0 staminaEvent:FireServer('sprint', 16) if currStamina <= 0 then wait(2) else wait(1) end while not userInputService:IsKeyDown(Enum.KeyCode.LeftShift) and currStamina < 100 do currStamina = currStamina + 1 updateStamina() wait(0.4) if currStamina > 100 then currStamina = 100 updateStamina() end end end)
Hope this works!