i tried to use a tutorial on yt about how to make a stamina and sprinting system. heres the vid: ROBLOX Sprint + Stamina Bar - 2019 Scripting Tutorial (Simple Endurance/Run Bind) - YouTube
the script does work, but not correctly, it does speed up the player, but doesnt lower the stamina bar. the stamina bar is a frame inside a screen gui inside startergui.
i have two scripts, a local script inside startercharacterscripts
staminaclient script:
local uis = game:GetService(“UserInputService”)
local repstore = game:GetService(“ReplicatedStorage”)
local players = game:GetService(“Players”)
– sprint key presseduis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
repstore.RemoteEvents.Sprint:FireServer(“Began”)
end
end)– sprint key ended
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
repstore.RemoteEvents.Sprint:FireServer(“Ended”)
end
end)repstore.RemoteEvents.StaminaUpdate.OnClientEvent:Connect(function(stamina, maxStamina)
players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new((stamina / maxStamina)* 0,300, 0,25)
end)
and a serverscript
staminaserver script:
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local players = game:GetService(“Players”)
local runService = game:GetService(“RunService”)local maxStamina = 500
local staminaRegen = 2local sprintModifier = 1.8
local sprintStaminaCost = 5local sprintingplayers = {
}
players.PlayerAdded:Connect(function(player)
local stamina = Instance.new(“IntValue”, player)
stamina.Value = maxStamina
stamina.Name = “Stamina”stamina.Changed:Connect(function(property)
replicatedStorage.RemoteEvents.StaminaUpdate:FireClient(player, stamina.Value, maxStamina)
end)
end)– sprint key pressed or released
replicatedStorage.RemoteEvents.Sprint.OnServerEvent:Connect(function(player, state)
local hum = player.Character.Humanoidif state == “Began” and hum:GetState() == Enum.HumanoidStateType.RunningNoPhysics and hum.MoveDirection.Magnitude > 0 then
sprintingplayers[player.Name] = hum.WalkSpeed
hum.WalkSpeed = hum.WalkSpeed * sprintModifier
elseif state == “Ended” and sprintingplayers[player.Name] then
hum.WalkSpeed = sprintingplayers[player.Name]
sprintingplayers[player.Name] = nilend
end)–adjust stamina
runService.Heartbeat:Connect(function()
for index, player in pairs(players:GetChildren()) do
local stamina = player.Stamina
local name = player.Nameif not sprintingplayers[name] then if stamina.Value > maxStamina then stamina.Value = maxStamina elseif stamina.Value < maxStamina then stamina.Value = stamina.Value + staminaRegen end else if stamina.Value >= sprintStaminaCost then stamina.Value = stamina.Value - sprintStaminaCost else player.Character.Humanoid.WalkSpeed = sprintingplayers[name] sprintingplayers[name] = nil end end
end
end)
im not sure what is wrong, please help.