What this script does is increase your magic until you release the key bind/you magic is full but when the player stops holding the key bind the loop still continues. How can I avoid this?
Server:
local ReplicatedS = game:GetService("ReplicatedStorage")
local WitchEffects = ReplicatedS.CharacterFiles.WitchEffects
local WitchEvents = ReplicatedS.CharacterFiles.WitchEvents
local PainEvent = WitchEvents:WaitForChild("selfsiphon")
local ts = game:GetService("TweenService")
local uis=game:GetService("UserInputService")
PainEvent.OnServerEvent:Connect(function(player,signal,script)
local ReplicatedS = game:GetService("ReplicatedStorage")
local WitchEffects = ReplicatedS.CharacterFiles.WitchEffects
local WitchEvents = ReplicatedS.CharacterFiles.WitchEvents
local PainEvent = WitchEvents:WaitForChild("selfsiphon")
local ts = game:GetService("TweenService")
local uis=game:GetService("UserInputService")
print(signal)
if signal == "Began" then
local MAGICREGEN_RATE = 3.2/10
local ENERGYTAKE_RATE = 2/10
local MAGICREGEN_STEP = 0
local Character = player.Character
local CharacterConfig = player.Character:WaitForChild("CharacterConfig")
local Magic = CharacterConfig:WaitForChild("Magic")
local MaxMagic = Magic:WaitForChild("MaxMagic")
local Energy = CharacterConfig:WaitForChild("Energy")
local MaxEnergy = Energy:WaitForChild("MaxEnergy")
local magicinc = 22
local random = math.random(1,3)
local animID = nil
if random == 1 then
animID= "rbxassetid://9884476843"
elseif random == 2 then
animID= "rbxassetid://9872595466"
elseif random == 3 then
animID="rbxassetid://9884539696"
end
local anim2 = Instance.new("Animation")
anim2.AnimationId = animID
local playAnim2 = player.Character:WaitForChild("Humanoid"):LoadAnimation(anim2)
playAnim2:Play()
local siphon,siphon2 = WitchEffects.Siphon1:Clone(),WitchEffects.Siphon2:Clone()
siphon.Parent = player.Character.RightHand
siphon2.Parent = player.Character.RightHand
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://10163066537"
Sound.Looped = false
Sound.RollOffMinDistance = 10
Sound.RollOffMaxDistance = 100
Sound.Name = "telekSound"
Sound.Volume = 3
Sound.Parent = player.Character.Head
Sound:Play()
local runLoop = true
repeat
wait(0.1)
if signal == "Ended" then
runLoop = false
end
local dt = wait(MAGICREGEN_STEP)
local dh = dt*MAGICREGEN_RATE*MaxMagic.Value
local dh2 = dt*ENERGYTAKE_RATE*MaxEnergy.Value
Magic.Value = math.min(Magic.Value + dh, MaxMagic.Value)
Energy.Value = math.min(Energy.Value - dh2, MaxEnergy.Value)
until runLoop == false or player.Character.Humanoid.Health == 0 or Magic.Value == MaxMagic.Value or Energy.Value <= 0
if Energy.Value < 0 then
Energy.Value = 0
end
Sound:Destroy()
siphon.Enabled = false
siphon2.Enabled = false
playAnim2:Stop()
wait(1)
siphon2:Destroy()
siphon:Destroy()
anim2:Destroy()
end
end)
Local:
--[[ Varaibles ]]--
local uis=game:GetService("UserInputService")
local players = game.Players
local player = players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedS = game:GetService("ReplicatedStorage")
local WitchEvents = ReplicatedS.CharacterFiles.WitchEvents
local PainEvent = WitchEvents:WaitForChild("selfsiphon")
local Debounce = true
local CharacterConfig = player.Character:WaitForChild("CharacterConfig")
--[[ Main ]]--
-- pressing
uis.InputBegan:connect(function(input)
if input.KeyCode==Enum.KeyCode.Z and CharacterConfig.Magic.Value < CharacterConfig.Magic.MaxMagic.Value and CharacterConfig.Energy.Value > 0 then
PainEvent:FireServer("Began", script)
end
end)
-- left
uis.InputEnded:connect(function(input)
if input.KeyCode==Enum.KeyCode.Z then
PainEvent:FireServer("Ended", script)
end
end)