Jetpack Velocity Issues

Hello! I am Domderp999. The Creator of a future game, “Volcano Madness,” In it, there is a jetpack… Currently I am struggling with BodyVelocity issues. When I equip the Jetpack tool, It works normally, but when I un-equip it and equip it again, the BodyVelocity’s vector3 doubles … And when i unequip and equip more and more, the body velocity’s value gets higher. The Code:

script.Parent.Equipped:Connect(function()
	---------------
	-- Variables --
	
	local FlightSpeed = 75
	local ThrustPower = 10000
	
	-- Variables
	---------------
	
	local bodyForce = Instance.new("BodyForce")
	local Player = game.Players.LocalPlayer
	bodyForce.Force = Vector3.new(0,0,0)
	bodyForce.Parent = script.Parent.Handle
	
	local UIS = game:GetService("UserInputService")
	UIS.InputBegan:Connect(function(keyCode)
		if keyCode.keyCode == Enum.KeyCode.Space then
			bodyForce.Force = Vector3.new(0,ThrustPower,0)
			Player.Character.Humanoid.WalkSpeed = FlightSpeed
		end
	end)

	UIS.InputEnded:Connect(function(keyCode)
		if keyCode.keyCode == Enum.KeyCode.Space then
			bodyForce.Force = Vector3.new(0,0,0)
			Player.Character.Humanoid.WalkSpeed = 16
		end
	end)
end)```

--What is my issue? Im unsure, but I think its this line :

bodyForce.Force = Vector3.new(0,ThrustPower,0)

--And if you find that that is the issue, how could I fix it?

Sounds like an event connection issue as every time you equip you to create a new BodyForce Instance which stack up I believe.

Also, I don’t see this instance being destroyed once the tool is unequipped, so maybe check if what I said is correct?

Edit:

Also everytime you equip the tool you connect to the UserInputServices.

These events can stack up after a long time and create lag so just be aware of that unless you are using another way to remove those events like :Destroy(), please refer to the dev reference API for how to handle event connections.

1 Like