I use a function that gives the player the ability to fly. It is quiet long so I don’t want to post the whole thing, but this code here is basically all the reference that should be needed in this post.
humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
if humanoid.JumpPower == 100 then
Flying = true
-- rest of flying code
Basically I have a few functions inside of this that set flying = false, ending the flying state. These are if the player jumps, if the player touches something, etc.
However after PropertyChangedSignal and JumpPower == 100 they begin running, and they continue running even after the Flying = false.
To get around this, I want to put the entire flying function into a Connection. I am not sure if this is the best choice for me to make or if there are other fixes that I am not seeing, but I figured that if I use something like if JumpPower ~= 100 then, Connection:Disconnect().
I figured that this would be a very easy way of resolving the issues that I am having, but is seems that I cannot simply Connection = humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
my way to the solution.
When I do this, the function does not play out at all.
I am not sure if that is possible with how my code is structured.
Summary
local Flying = false
-- setting flycon to the humanoid:propertychange doesn't work for some reason
local flycon
humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
if humanoid.JumpPower == 100 then
delay(5, function()
Flying = false
end)
Flying = true
print("Power Change Acknowledged")
wait(.5)
print("Flying State Began")
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"
if Flying == true and StateTracker.State ~= "onRunning" then
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.Space then
Flying = false
print("flying false do to jump")
end
end)
for i, v in pairs(character:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(touch)
if not character:FindFirstChild(touch.Name) then
Flying = false
print("flying false do to touch")
end
end)
end
end
end
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
wait(.5)
flylatch()
local Gravity = Controller.GravityUp
if (Gravity-Vector3.new(0, 1, 0)).Magnitude > .1 then
Flying = false
print("flying false do to gravity")
end
else
Connection:Disconnect()
BV:Destroy()
character.Animate.Disabled = false
humanoid.JumpPower = 0
end
end)
if not Suc and Err then
print(Err)
Connection:Disconnect()
BV:Destroy()
character.Animate.Disabled = false
humanoid.JumpPower = 0
end
end)
else
Flying = false
end
end)
In the original post I said my reason for wanting to create this connect was because I have functions that disable flying within this code, that do not stop working after flying ends.
These are the functions that I mean:
Summary
if Flying == true and StateTracker.State ~= "onRunning" then
UIS.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.Space then
Flying = false
print("flying false do to jump")
end
end)
for i, v in pairs(character:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(touch)
if not character:FindFirstChild(touch.Name) then
Flying = false
print("flying false do to touch")
end
end)
end
end
end
I believe that I am doing what you suggested with these statements already, but they do not stop after they no longer fall within the if-statement.