ContextActionService keeps going even when not pressing the button

So I was making my fly script work on mobile and I was trying to use Context Action Service and when I press the binded key once I fly but once I unequipped the tool, and activated it I don’t need to press the binded anymore. Which is annoying since if you don’t want to go forward you have to.---- Also when the binded key is not down you still keep flying forward.

Any help with these two problems will be appreciated!

local Tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local HumanRootPart = character:WaitForChild("HumanoidRootPart")
local cam = game.Workspace.CurrentCamera
local Debris = game:GetService("Debris")
local ContextActionS = game:GetService("ContextActionService")
local pressed = false
local BodyVel 
local BodyGyro  
local Force = 75000


local WPressed = "WFoward"
local function foward(ActionName, InputState)
	if ActionName == WPressed and InputState == Enum.UserInputState.Begin then
		cam:GetPropertyChangedSignal("CFrame"):Connect(function()
			if InputState == Enum.UserInputState.Begin then
			BodyVel.Velocity = cam.CFrame.LookVector * 65
			if BodyGyro and BodyGyro.Parent then
					BodyGyro.CFrame = cam.CFrame
				end
			end
		end)
		print("began")
	elseif InputState == Enum.UserInputState.End then
		BodyVel.Velocity = Vector3.new(0,0,0)
		print("ended")
	end
end

Tool.Activated:Connect(function()
	if not pressed then
		pressed = true
		BodyVel = Instance.new("BodyVelocity")
		BodyGyro = Instance.new("BodyGyro")
		BodyGyro.Parent = HumanRootPart
		BodyGyro.MaxTorque = Vector3.new(1, 1, 1)*10^6;
		BodyGyro.P = 10^6;
		BodyVel.Parent = HumanRootPart
		BodyVel.P =  10^4;
		BodyVel.MaxForce = Vector3.new(Force,Force,Force)
		BodyVel.Velocity = Vector3.new(0,0,0)
		ContextActionS:BindAction(WPressed, foward, true, Enum.KeyCode.W)
	elseif pressed then
		pressed = false
		ContextActionS:UnbindAction(WPressed)
		BodyGyro:Destroy()
		BodyVel:Destroy()
	end
end)

Tool.Unequipped:Connect(function()
	BodyVel:Destroy()
	ContextActionS:UnbindAction(WPressed)
end)

You need to store the camera CFrame change connection to a variable and disconnect it when it isn’t needed anymore. For example you could declare a variable called camConn. Declare it in the scope visible to the whole script (after Force, for example). Then, store the connection object returned by :Connect to a variable and call its disconnect method whenever you unbind the action or the input ends.

This is how you can disconnect it.

if camConn then
	camConn:Disconnect()
	camConn = nil
end	
1 Like

Thanks for the help but on mobile I won’t even move. If you don’t mind is there any way to fix this?