How do I stop something while its happening?

Got this run script/Move set script here:

How do I stop the player running when their energy reaches 0?

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
end)
local StopRunning = function(IsVampire)

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

end
-- I'm assuming BarFolder.Energy is a NumberValue/IntValue
VampireEnergy.Changed:Connect(function()

	if VampireEnergy.Value > 0 then return end -- If you have energy, dont run this script
	StopRunning(true)

end

Energy.Changed:Connect(function()

	if Energy.Value > 0 then return end -- If you have energy, dont run this script
	StopRunning(false)

end

How about this? Also you might want to use this function when Key == “leftShiftEnded” too

When you script something twice, you’re doing it wrong. Use functions.

You should probably also load all the following before your KeyPressed function.
If you do all of this every single time a key is pressed you’re going to probably run into lag issues.

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

[/quote]

1 Like

if you would like a script to stop while it is running, use return.

I would but it is a server script, I dont know how to get the player before the function/remote event.

Maybe get all that information on the local script that fired the event, then give it to the server as a parameter

example

local Player = game:GetService("Players").LocalPlayer
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

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

RemoteEvent:FireServer(Classes)

1 Like

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