Spaming Shift Recharges Mana Really Fast

I have a recharge time for the mana but for some reason when you spam the shift button you get mana really fast. Please Help.

local userInput = game:getService('UserInputService')
local Players = game:GetService('Players')

local walkSpeed = 16

local ManaChargeTime = 0.5
local player = Players.LocalPlayer
local Class = player:WaitForChild("Stats").Class
local Mana = player:WaitForChild("Stats").Mana
local maxMana = player:WaitForChild("Stats").MaxMana
local running = false
local isWalking

local function beginSprint(input, gameProcessed)
	player.Character.Humanoid.Running:connect(function(speed)
		if speed > 0 then isWalking = true
		elseif speed < 1 then
			isWalking = false
		end
	end)
	if Class.Value == "Light" then
		if not gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftShift then
					if Mana.Value >= 1 and isWalking == true then
						local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
						running = true
						player.Character.Humanoid.WalkSpeed = sprintSpeed
						while Mana.Value > 0 and running do
							if isWalking == false then
								player.Character.Humanoid.WalkSpeed = walkSpeed
								task.wait(ManaChargeTime)
								if Mana.Value > 0 and running then
									Mana.Value = Mana.Value + 1
								end
							elseif isWalking == true then
								local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
								player.Character.Humanoid.WalkSpeed = sprintSpeed
								task.wait(0.2)
								if Mana.Value > 0 and running then
									Mana.Value = Mana.Value - 1
									if Mana.Value == 0 then
										player.Character.Humanoid.WalkSpeed = walkSpeed
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

local function endSprint(input, gameProcessed)
	if Class.Value == "Light" then
		if not gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftShift then
					local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
					running = false
					player.Character.Humanoid.WalkSpeed = walkSpeed
					while task.wait(ManaChargeTime) and Mana.Value < maxMana.Value and not running do
						Mana.Value = Mana.Value + 1
						if Mana.Value == 0 then
							player.Character.Humanoid.WalkSpeed = walkSpeed
						end
					end
				end
			end
		end
	end
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)
2 Likes
local userInput = game:getService('UserInputService')
local Players = game:GetService('Players')

local walkSpeed = 16

local ManaChargeTime = 0.5
local player = Players.LocalPlayer
local Class = player:WaitForChild("Stats").Class
local Mana = player:WaitForChild("Stats").Mana
local maxMana = player:WaitForChild("Stats").MaxMana
local running = false
local isWalking

local function beginSprint(input, gameProcessed)
	player.Character.Humanoid.Running:connect(function(speed)
		if speed > 0 then isWalking = true
		elseif speed < 1 then
			isWalking = false
		end
	end)
	if Class.Value == "Light" then
		if not gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftShift then
					if Mana.Value >= 1 and isWalking == false then
						local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
						running = true
						player.Character.Humanoid.WalkSpeed = sprintSpeed
						while Mana.Value > 0 and running do
							if isWalking == false then
								player.Character.Humanoid.WalkSpeed = walkSpeed
								task.wait(ManaChargeTime)
								if Mana.Value > 0 and running then
									Mana.Value = Mana.Value + 1
								end
							elseif isWalking == true then
								local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
								player.Character.Humanoid.WalkSpeed = sprintSpeed
								task.wait(0.2)
								if Mana.Value > 0 and running then
									Mana.Value = Mana.Value - 1
									if Mana.Value == 0 then
										player.Character.Humanoid.WalkSpeed = walkSpeed
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

local function endSprint(input, gameProcessed)
	if Class.Value == "Light" then
		if not gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftShift then
					local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
					running = false
					player.Character.Humanoid.WalkSpeed = walkSpeed
					while task.wait(ManaChargeTime) and Mana.Value < maxMana.Value and not running do
						Mana.Value = Mana.Value + 1
						if Mana.Value == 0 then
							player.Character.Humanoid.WalkSpeed = walkSpeed
						end
					end
				end
			end
		end
	end
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)

The reason was that you give the player stamina even when they are running.
replace it with this(a small change at around line 25(?)
This should fix it, i think.

3 Likes

I just tested it but now it won’t let me sprint and the mana does not go down.

1 Like

image
Oh my!

I suggest you just scrap all that, and then launch a loop that replenishes your mana as long as some variable is true:

local manaRecharge = false

userInput.InputBegan:Connect(function(input, busy)
   if busy then return end

   if input == Enum.KeyCode.LeftShift then
      manaRecharge = true
   end
end)

userInput.InputEnded:Connect(function(input, busy)
   if input == Enum.KeyCode.LeftShift then
      manaRecharge = false
   end
end)

while task.wait() do
   if manaRecharge then
      Mana.Value += 1
   end
end
2 Likes

That is true and would make it way simpler, I suggest you try this one out first.

2 Likes

I never thought about it that way but I am still having trouble finding out where to put that code and what to take out because now nothing works. (Also I am sorry to bother you)

local userInput = game:getService('UserInputService')
local Players = game:GetService('Players')

local walkSpeed = 16

local ManaChargeTime = 0.5
local player = Players.LocalPlayer
local Class = player:WaitForChild("Stats").Class
local Mana = player:WaitForChild("Stats").Mana
local maxMana = player:WaitForChild("Stats").MaxMana
local running = false
local isWalking

local manaRecharge = false

userInput.InputBegan:Connect(function(input, busy)
	if busy then return end

	if input == Enum.KeyCode.LeftShift then
		manaRecharge = true
	end
end)

userInput.InputEnded:Connect(function(input, busy)
	if input == Enum.KeyCode.LeftShift then
		manaRecharge = false
	end
end)

while task.wait() do
	if manaRecharge then
		Mana.Value += 1
	end
end

local function beginSprint(input, gameProcessed)
	if Class.Value == "Light" then
		if not gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftShift then
					if Mana.Value >= 1 then
						local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
						player.Character.Humanoid.WalkSpeed = sprintSpeed
					end
				end
			end
		end
	end
end

local function endSprint(input, gameProcessed)
	if Class.Value == "Light" then
		if not gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local keycode = input.KeyCode
				if keycode == Enum.KeyCode.LeftShift then
					local sprintSpeed = player:WaitForChild("Stats").SpeedValue.Value
					player.Character.Humanoid.WalkSpeed = walkSpeed
				end
			end
		end
	end
end

userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)
2 Likes

Oh, whoops, my code wasn’t entirely correct, here’s the corrected version:

local manaRecharge = false

userInput.InputBegan:Connect(function(input, busy)
   if busy then return end

   if input.KeyCode == Enum.KeyCode.LeftShift then
      manaRecharge = true
   end
end)

userInput.InputEnded:Connect(function(input, busy)
   if input.KeyCode == Enum.KeyCode.LeftShift then
      manaRecharge = false
   end
end)

while task.wait() do
   if manaRecharge then
      Mana.Value += 1
   end
end

And since there’s a loop at the end, you might want to put it in a separate thread since right now it blocks off the rest of the script. You can also just try moving your sprinting logic.

3 Likes

I want to separate the code into another script but should it still be a local script in the starter player scripts folder?

2 Likes

Also this happens when I sprint…

2 Likes

Yeah, because you don’t constrain the mana value. You should clamp it so that it doesn’t go above the maximum value.

2 Likes

How exactly do I do that? I don’t know how.

2 Likes

You can use the math library for that, here are definitions for all the functions, including clamp:

2 Likes

Thank you but if I used math.clamp where would I put It.

2 Likes

Put it where you change the mana value.

2 Likes

I am going to rewrite the script and use everything that you told me. Thank you so much for your help

2 Likes