You should use runservice instead of wait loops, this would make it much more effective.
If you are trying to make a run script with a stamina meter, it would be best to do this,
local runservice = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local value = Instance.new("NumberValue",player) value.Name = "Stamina" -- you can change this if you need to
local valuemax = 100-- what it regains too (change if you want)
local valuemin = 0-- what it drains too (change if you want)
local drainrate = 10-- change if you want (the rate is 1 per-second)
local gainrate = 35-- change if you want (the rate is 1 per-second)
local ispaused = false-- controls when it's paused (not too sure why you need this though..)
local isdraining = false-- controls when it's draining (use this for input)
local gainwait = 1.5-- how long it waits to regain (change if you want)
local _g = 0-- do not touch..
value.Value = valuemax
runservice.RenderStepped:Connect(function(delta)-- you could change this to heartbeat if you'd like
if ispaused == true then
delta = 0
end
local _drate = drainrate*delta
local _grate = gainrate*delta
local _grate2 = gainwait*delta
local oldvalue = tonumber(value.Value) or valuemax
local newvalue = oldvalue
local isgaining = false
if not isdraining and newvalue < valuemax then
_g = math.min(gainwait,_g+_grate2)
if _g >= gainwait then
isgaining = true
end
elseif isdraining then
_g = 0
end
if isdraining then
newvalue = math.clamp(newvalue-_drate,valuemin,valuemax)
end
if isgaining then
newvalue = math.clamp(newvalue+_grate,valuemin,valuemax)
end
value.Value = newvalue
end)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(i)
if i.KeyCode == Enum.KeyCode.E then
isdraining = not isdraining
end
end)
However, I went through the trouble of making the entire script for you, here you go!
local runservice = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local value = Instance.new("NumberValue",player) value.Name = "Stamina" -- you can change this if you need to
local valuemax = 100-- what it regains too (change if you want)
local valuemin = 0-- what it drains too (change if you want)
local drainrate = 10-- change if you want (the rate is 1 per-second)
local gainrate = 35-- change if you want (the rate is 1 per-second)
local ispaused = false-- controls when it's paused (not too sure why you need this though..)
local isdraining = false-- controls when it's draining (use this for input)
local gainwait = 1.5-- how long it waits to regain (change if you want)
local _g = 0-- do not touch..
local screen = Instance.new("ScreenGui",player:WaitForChild("PlayerGui"))-- you can change this if you'd like
screen.ResetOnSpawn = false
local frame = Instance.new("Frame",screen)-- you can change this too if you'd like
-- you can customize all of this
local frameFullSize = UDim2.new(1,0,0,20)
local frameEmptySize = UDim2.new(0,0,0,20)
local frameFullColor = Color3.new(0,0.65,1)
local frameEmptyColor = Color3.new(0,0.65,1)
-- i would touch anything else after this (unless you know what you're doing..)
frame.BorderSizePixel = 4
frame.Size = frameFullSize
frame.AnchorPoint = Vector2.new(0,0.5)
frame.Position = UDim2.new(0,0,1,0)
frame.BackgroundColor3 = Color3.new(0,0.65,1)
function SetBarRatio(newratio)
newratio = math.clamp(tonumber(newratio) or 1,0,1)
frame.Size = frameEmptySize:Lerp(frameFullSize,newratio)
frame.BackgroundColor3 =frameEmptyColor:Lerp(frameFullColor,newratio)
end
value.Value = valuemax
runservice.RenderStepped:Connect(function(delta)-- you could change this to heartbeat if you'd like
if ispaused == true then
delta = 0
end
local _drate = drainrate*delta
local _grate = gainrate*delta
local _grate2 = gainwait*delta
local oldvalue = tonumber(value.Value) or valuemax
local newvalue = oldvalue
local isgaining = false
if not isdraining and newvalue < valuemax then
_g = math.min(gainwait,_g+_grate2)
if _g >= gainwait then
isgaining = true
end
elseif isdraining then
_g = 0
end
if isdraining then
newvalue = math.clamp(newvalue-_drate,valuemin,valuemax)
end
if isgaining then
newvalue = math.clamp(newvalue+_grate,valuemin,valuemax)
end
local ratio = newvalue/valuemax
SetBarRatio(ratio)
value.Value = newvalue
if newvalue <= valuemin then
Run(false)
end
end)
local runbuttons = {Enum.KeyCode.LeftShift}
local runspeed = 32-- change this if you'd like
local walkspeed = 16-- you could change this as well
local uis = game:GetService("UserInputService")
local isrunning = false
local lasthum = nil
local lastchar = nil
-- addd or remove states if you want
local okaystates = {Enum.HumanoidStateType.Running,Enum.HumanoidStateType.RunningNoPhysics,Enum.HumanoidStateType.Jumping,
Enum.HumanoidStateType.Landed, Enum.HumanoidStateType.Freefall
}
local lastrunspeed = 0
function Run(bool)
local hum = lasthum
if hum ~= nil then
if isrunning then
if value.Value > valuemin then
hum.WalkSpeed = runspeed
local state = hum:GetState()
isdraining = ((table.find(okaystates,state)) ~= nil) and lastrunspeed >= (math.max(walkspeed,runspeed)-5)
else
isrunning = false
end
else
hum.WalkSpeed = walkspeed
isdraining = false
end
else
isdraining = false
end
end
function onInput(keycode)
if uis:GetFocusedTextBox() ~= nil then
Run(false)
isrunning = false
return
end
local found = false
if keycode == nil then
for i,v in pairs(runbuttons) do
if uis:IsKeyDown(v) then
found = true
break
end
end
else
found = uis:IsKeyDown(keycode)
end
isrunning = found == true and value.Value > valuemin
Run(isrunning)
end
function OnCharAdded(char)
local hum = char:WaitForChild("Humanoid")
lasthum = hum
Run(isrunning)
hum.StateChanged:Connect(function()
Run(isrunning)
end)
hum.Running:Connect(function(speed)
lastrunspeed = speed
Run(isrunning)
end)
end
uis.InputBegan:Connect(function(input)
if table.find(runbuttons,input.KeyCode) then
onInput(input.KeyCode)
end
end)
uis.InputEnded:Connect(function(input)
if table.find(runbuttons,input.KeyCode) then
onInput(input.KeyCode)
end
end)
uis.TextBoxFocused:Connect(function()
onInput()
end)
uis.TextBoxFocusReleased:Connect(function()
onInput()
end)
player.CharacterAdded:Connect(OnCharAdded)
You may customize it as much as you’d like, enjoy! (ps. let me know if there’s any errors and stuff)