How to add Flying code to the script; How to end Flying State

Hi Guys,

Basically I made a charged jump script; if the player holds Space for 1 second then they jump twice as high.

I now want to add 5 seconds of flight when they reach the peak of the jump. I already have this part figured out, but I am unsure of how to successfully add the flying part of the code into the script.

(I basically want this from Shindo Life)

This is the code that I currently have; It is all working, I am just unsure of how to end it after 5 seconds:

Summary
local flying = false
local cam = workspace.CurrentCamera

humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
	if humanoid.JumpPower == 100 then
		print("Power Change Acknowledged")
		wait(.75)
		print("Flying State Began")
		
		flying = true
			
		character.Animate.Disabled = true
		-- ADD FLYING ANIMATION HERE
			
		local bv = Instance.new("BodyVelocity", character.PrimaryPart)
		bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		bv.Velocity = Vector3.new(0,0,0)
		bv.Name = "FlightForce"
		
		while wait() do
			if flying then
				character.PrimaryPart:FindFirstChild("FlightForce").Velocity = Vector3.new(0,0,0)
				character.PrimaryPart:FindFirstChild("FlightForce").Velocity = cam.CFrame.LookVector * 50
			end
		end	
	end
end)

This is what it currently looks like; it just never ends:
https://gyazo.com/17231ee5547c165fe0d52d0f5e852b86

2 Likes

It’s better to connect a RunService.Heartbeat event to an function. Wait(), in most, if not all times, isn’t reliable. And so, you can find more about it here: https://eryn.io/gist/3db84579866c099cdd5bb2ff37947cec

Also, right at the start, you’d add an delay function. (Delay explained:

void delay ( number delayTime, function callback )
Schedules a function to be executed after delayTime seconds have passed, without yielding the current thread. This function allows multiple Lua threads to be executed in parallel from the same stack. The delay will have a minimum duration of 29 milliseconds, but this minimum may be higher depending on the target framerate and various throttling conditions. If the delayTime parameter is not specified, the minimum duration will be used.

Source: Roblox Globals
)

With that in mind, we would put 5 as the delayTime, and the callback function would disable flying.

Although this is spoon-feeding, here’s what I would do:

local Flying = false
local Cam = workspace.CurrentCamera

Hum:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
	if Hum.JumpPower == 100 then
		delay(5, function()
			Flying = false
		end)
		print("Power Change Acknowledged")
		wait(.75)
		print("Flying State Began")

		Flying = true

		character.Animate.Disabled = true
		-- ADD FLYING ANIMATION HERE

		local BV = Instance.new("BodyVelocity", character.PrimaryPart)
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BV.Velocity = Vector3.new(0,0,0)
		BV.Name = "FlightForce"
		
		local Connection
		
		Connection = game:GetService('RunService').Heartbeat:Connect(function()
			local Suc, Err = pcall(function()
				if Flying then
					BV.Velocity = Vector3.new(0,0,0)
					BV.Velocity = Cam.CFrame.LookVector * 50
				else
					Connection:Disconnect()
				end
			end)
			
			if not Suc and Err then
				print(Err)
				Connection:Disconnect()
			end
		end)
	end
end)

Now, why a pcall / protected call? Well, it’s simple:

If the code gives a error, it will spam the output and if you have a slow device, it might cause lag. But still, even with a good device, it might also do. And by using protected calls, you can check if it gave a error, and if so, you can stop the RunService.Heartbeat connection, avoiding unwanted spammed error messages.

Note:
Delay() is not 100% accurate. But if you wish so, you could use Promise’s Delay

3 Likes

Right as I was reading through the explanations lol

1 Like

I am not exactly sure what that means.

The time delay seems to be working; I added a Print to it.

delay(5, function()
			Flying = false
			print("5 seconds over")
		end)

However, the flying state does not disable by setting Flying = false

Edit:

The code is working currently, I just need to create a variable of the character’s normal velocity and have it revert back to that when Flying = false

2 Likes

So I was able to resolve this issue thanks to help from @PhoenixRessusection with adding the delay function, and the pcall:

Here is the working script; It is based off another script that changes JumpPower to 100, so if you are wanting to use this script then it may not work for you because of that:

Summary
local Flying = false
local Cam = workspace.CurrentCamera

humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
	if humanoid.JumpPower == 100 then
		delay(5, function()
			Flying = false
		end)
		print("Power Change Acknowledged")
		wait(.5)
		print("Flying State Began")

		Flying = true

		character.Animate.Disabled = true
		-- ADD FLYING ANIMATION HERE

		local BV = Instance.new("BodyVelocity", character.PrimaryPart)
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BV.Velocity = Vector3.new(0,0,0)
		BV.Name = "FlightForce"
		
		UIS.InputBegan:Connect(function(input, gameProcessed)
			if input.KeyCode == Enum.KeyCode.Space then
				Flying = false
			end
		end)	
		
		local Connection

		Connection = game:GetService('RunService').Heartbeat:Connect(function()
			local Suc, Err = pcall(function()
				if Flying == true then
					BV.Velocity = Vector3.new(0,0,0)
					BV.Velocity = Cam.CFrame.LookVector * 50
				else
					Connection:Disconnect()
					BV:Destroy()
				end
			end)

			if not Suc and Err then
				print(Err)
				Connection:Disconnect()
			end
		end)
	end
end)

The main change that I made was to this section of @PhoenixRessusection’s code

Connection = game:GetService('RunService').Heartbeat:Connect(function()
			local Suc, Err = pcall(function()
				if Flying == true then
					BV.Velocity = Vector3.new(0,0,0)
					BV.Velocity = Cam.CFrame.LookVector * 50
				else
					Connection:Disconnect()
					BV:Destroy()
				end
			end)

I simply added BV:Destroy() if Flying=false, this stopped the flying state and reverted the humanoid back to its original, normal state.

I also added this code:

UIS.InputBegan:Connect(function(input, gameProcessed)
			if input.KeyCode == Enum.KeyCode.Space then
				Flying = false
			end
		end)	

This gives the Player an option to end the Flying State by themselves.

Edit:

The only thing this script is really missing is animations.

2 Likes