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)
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.
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
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)
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.