How do i fix spamming shift causing stamina to break on my script?

Here’s my code, I’m using loops for the stamina to work and I really don’t know how to fix it.
I also don’t want to use a debounce unless you think its the only answer. :sob: Thanks for your help!

local UserInput = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character
local frame = script.Parent:WaitForChild("SprintBar")
local sprintBar = frame:WaitForChild("SprintBar")
local sprintBarBack = frame:WaitForChild("SprintBarback")
local FTransparencyIn = game:GetService("TweenService"):Create(frame, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local FTransparencyOut = game:GetService("TweenService"):Create(frame, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local BTransparencyIn = game:GetService("TweenService"):Create(sprintBar, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local BTransparencyOut = game:GetService("TweenService"):Create(sprintBar, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local BBTransparencyIn = game:GetService("TweenService"):Create(sprintBarBack, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local BBTransparencyOut = game:GetService("TweenService"):Create(sprintBarBack, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local Sprinting = false
local hum = Char:WaitForChild("Humanoid")
local FOVenabled = true
local mx = 100
local stm = 100

UserInput.InputBegan:Connect(function(input, gameProc)
	if not gameProc then
		if input.UserInputType == Enum.UserInputType.Keyboard and sprintBar.Size ~= UDim2.new(0, 0, 0.76, 0) then
			if input.KeyCode == Enum.KeyCode.LeftShift then
				Sprinting = true
				script.Parent.InputEnd.Enabled = false
				BTransparencyOut:Play()
				BBTransparencyOut:Play()
				FTransparencyOut:Play()
				wait()
				script.Parent.InputEnd.Enabled = true
				if FOVenabled == true then
					game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 83 }):Play()
				end
				hum.WalkSpeed = 23 
				while Sprinting and stm > 0 do
					stm = stm - 0.5
					sprintBar.Size = UDim2.new(stm / mx, 0, 0.76, 0)
					wait(0.01)
				end
			end
		end
	end
end)

sprintBar:GetPropertyChangedSignal("Size"):Connect(function()
	if sprintBar.Size == UDim2.new(0, 0, 0.76, 0) then
		hum.WalkSpeed = 16
		if FOVenabled == true then
			print(FOVenabled)
			game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 70 }):Play()
		end
	end
end)

UserInput.InputEnded:Connect(function(input, gameProc)
	if not gameProc then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
			Sprinting = false
			BTransparencyOut:Cancel()
			BBTransparencyOut:Cancel()
			FTransparencyOut:Cancel()
			wait(2)
			while not Sprinting and stm < 100 do
				stm = stm + 0.5
				sprintBar.Size = UDim2.new(stm / mx, 0, 0.76, 0)
				wait(0.01)
			end
		end
	end
end)

script.Parent:WaitForChild("zoom").Event:Connect(function(m)
	FOVenabled = m
	script.Parent.zoom:WaitForChild("Value").Value = m
end)

7 Likes

a debounce is the only way (that i can think of LOL) :

local Cooldown = false ← at the start

then at InputBegan :

if input.KeyCode == Enum.KeyCode.LeftShift then
Sprinting = true
Cooldown = false

& deactivate the cooldown at the end of InputEnded :

		     end
			cooldownActive = false -- stop spamming
		end
	end
end)

Also a tip :

Have you considered making a StaminaLoop function to store your regenerations like so :

local function StaminaLoop()
	while true do
		if Sprinting and stm > 0 then
			stm = stm - 0.5
		elseif not Sprinting and stm < 100 then
			stm = stm + 0.5
		end

		sprintBar.Size = UDim2.new(stm / mx, 0, 0.76, 0)
		wait(0.01)
	end
end

and then looping it on a Heartbeat? Like this :

RunService.Heartbeat:Connect(StaminaLoop)

That’ll ensure that the regeneration occurs consistently & wont affect your script

2 Likes

Okie :ok_hand:. What must be done, must be done​:+1:. Thanks!

1 Like

yes i tried using heartbeat but it caused the bar to lag

1 Like

I tried adding a debounce the way you did it and it still causes the sprint bar to go super saiyan.:fire:. I do think that a debounce will fix the problem though. :smirk:

local UserInput = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character
local frame = script.Parent:WaitForChild("SprintBar")
local sprintBar = frame:WaitForChild("SprintBar")
local sprintBarBack = frame:WaitForChild("SprintBarback")
local FTransparencyIn = game:GetService("TweenService"):Create(frame, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local FTransparencyOut = game:GetService("TweenService"):Create(frame, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local BTransparencyIn = game:GetService("TweenService"):Create(sprintBar, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local BTransparencyOut = game:GetService("TweenService"):Create(sprintBar, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local BBTransparencyIn = game:GetService("TweenService"):Create(sprintBarBack, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local BBTransparencyOut = game:GetService("TweenService"):Create(sprintBarBack, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local Sprinting = false
local hum = Char:WaitForChild("Humanoid")
local FOVenabled = true
local mx = 100
local stm = 100
local db = false



UserInput.InputBegan:Connect(function(input, gameProc)
	if not gameProc then
		if input.UserInputType == Enum.UserInputType.Keyboard and sprintBar.Size ~= UDim2.new(0, 0, 0.76, 0) then
			if input.KeyCode == Enum.KeyCode.LeftShift and db == false then  -- check for db
				Sprinting = true
				db = true  -- set db to true
				script.Parent.InputEnd.Enabled = false
				BTransparencyOut:Play()
				BBTransparencyOut:Play()
				FTransparencyOut:Play()
				wait()
				script.Parent.InputEnd.Enabled = true
				if FOVenabled == true then
					game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 83 }):Play()
				end
				hum.WalkSpeed = 23 
				while Sprinting == true and stm > 0 do
					stm = stm - 0.5
					sprintBar:TweenSize(UDim2.new(stm / mx, 0, 0.76, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.01)
					wait(0.01)
					
			end
				wait()
				db = false  -- wait after awhile to turn db back on after function finishes
		end
	end
end)

sprintBar:GetPropertyChangedSignal("Size"):Connect(function()
	if sprintBar.Size == UDim2.new(0, 0, 0.76, 0) then
		hum.WalkSpeed = 16
		if FOVenabled == true then
			print(FOVenabled)
			game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 70 }):Play()
		end
	end
end)

UserInput.InputEnded:Connect(function(input, gameProc)
	if not gameProc then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
			Sprinting = false
			BTransparencyOut:Cancel()
			BBTransparencyOut:Cancel()
			FTransparencyOut:Cancel()
			wait(3)
			while Sprinting == false and stm < 100 do
				stm = stm + 0.5
				sprintBar:TweenSize(UDim2.new(stm / mx, 0, 0.76, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.01)
				wait(0.01)
			end
		end
	end
end)

script.Parent:WaitForChild("zoom").Event:Connect(function(m)
	FOVenabled = m
	script.Parent.zoom:WaitForChild("Value").Value = m
end)

https://streamable.com/xzef66 Heres a video of me spamming :joy:

Sorry for the late reply i was gone, i rewrote your script using seperate functions in order to

  1. optimize it
  2. potentially fix the spamming issue you got
local UserInput = game:GetService("UserInputService")
local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character
local frame = script.Parent:WaitForChild("SprintBar")
local sprintBar = frame:WaitForChild("SprintBar")
local sprintBarBack = frame:WaitForChild("SprintBarback")
local FTransparencyIn = game:GetService("TweenService"):Create(frame, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local FTransparencyOut = game:GetService("TweenService"):Create(frame, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local BTransparencyIn = game:GetService("TweenService"):Create(sprintBar, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local BTransparencyOut = game:GetService("TweenService"):Create(sprintBar, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local BBTransparencyIn = game:GetService("TweenService"):Create(sprintBarBack, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 1 })
local BBTransparencyOut = game:GetService("TweenService"):Create(sprintBarBack, TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), { Transparency = 0 })
local Sprinting = false
local hum = Char:WaitForChild("Humanoid")
local FOVenabled = true
local mx = 100
local stm = 100
local debounce = false

local function StartSprinting()
	if not debounce and sprintBar.Size ~= UDim2.new(0, 0, 0.76, 0) then
		Sprinting = true
		debounce = true
		script.Parent.InputEnd.Enabled = false
		BTransparencyOut:Play()
		BBTransparencyOut:Play()
		FTransparencyOut:Play()
		wait()
		script.Parent.InputEnd.Enabled = true
		if FOVenabled then
			game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 83 }):Play()
		end
		hum.WalkSpeed = 23
		while Sprinting and stm > 0 do
			stm = stm - 0.5
			sprintBar:TweenSize(UDim2.new(stm / mx, 0, 0.76, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.01)
			wait(0.01)
		end
		wait()
		debounce = false
	end
end

local function StopSprinting()
	if not debounce and Sprinting then
		Sprinting = false
		debounce = true
		BTransparencyOut:Cancel()
		BBTransparencyOut:Cancel()
		FTransparencyOut:Cancel()
		wait(3)
		while not Sprinting and stm < 100 do
			stm = stm + 0.5
			sprintBar:TweenSize(UDim2.new(stm / mx, 0, 0.76, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.01)
			wait(0.01)
		end
		debounce = false
	end
end

UserInput.InputBegan:Connect(function(input, gameProc)
	if not gameProc then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
			StartSprinting()
		end
	end
end)

UserInput.InputEnded:Connect(function(input, gameProc)
	if not gameProc then
		if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
			StopSprinting()
		end
	end
end)

sprintBar:GetPropertyChangedSignal("Size"):Connect(function()
	if sprintBar.Size == UDim2.new(0, 0, 0.76, 0) then
		hum.WalkSpeed = 16
		if FOVenabled then
			game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), { FieldOfView = 70 }):Play()
		end
	end
end)

script.Parent:WaitForChild("zoom").Event:Connect(function(m)
	FOVenabled = m
	script.Parent.zoom:WaitForChild("Value").Value = m
end)
1 Like

Don’t include math, have a exact number for the speed. So it has no chance of breaking, because spamming multiple times makes it do the math multiple times