Running script not working properly

When you press shift my script will increase your speed, but there is an energy bar and if it hits 0 you stop running, or at least thats what is supposed to happen. The first time you press shift it will not care if you have 0 energy. But if you press shift, than stop then press it again it works.

script:

local KeyPressed = script.Parent.KeyPressed
--//Animations/--
local BloodSuck = script.BloodSuck


KeyPressed.OnServerEvent:Connect(function(Player, Key)
	local character = Player.Character or Player.CharacterAdded:Wait()	
	local humanoid = character:FindFirstChild("Humanoid")
	local BloodSuckTrack = humanoid:LoadAnimation(BloodSuck)

	local Vampire = Player:WaitForChild("Classes"):WaitForChild("Vampire")
	local Human = Player:WaitForChild("Classes"):WaitForChild("Human")

	local BarFolder = Player:WaitForChild("BarFolder")
	local Energy = BarFolder:WaitForChild("Energy")
	local VampireEnergy = BarFolder:WaitForChild("VampireEnergy")



	if Key == "G" then
		BloodSuckTrack:Play()

	elseif Key == "LeftShift" then
		if Vampire.Value == true then
			humanoid.WalkSpeed = 50
		elseif Human.Value == true then	
			humanoid.WalkSpeed = 23
		end	

	elseif Key == "LeftShiftEnded" then
		if Vampire.Value == true then
			humanoid.WalkSpeed = 30
		elseif Human.Value == true then
			humanoid.WalkSpeed = 16
		end	
	end

	while humanoid.WalkSpeed == 50 or humanoid.WalkSpeed == 23 do
		if Vampire.Value == true then
			VampireEnergy.Value -= 1
		elseif Human.Value == true then
			Energy.Value -= 1
		end
		wait(.1)
	end
	
	local StopRunning = function(IsVampire)

		if IsVampire then
			humanoid.WalkSpeed = 30
		elseif not IsVampire then
			humanoid.WalkSpeed = 16
		end	

	end
	
	VampireEnergy.Changed:Connect(function()

		if VampireEnergy.Value > 0 then return end
	
		StopRunning(true)
        
	end)

	Energy.Changed:Connect(function()

		if Energy.Value > 0 then return end
		
		StopRunning(false)
        
	end)
end)

I fixed it nevermind, it was a lodaing error or something

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.