local U=require(script.Parent:WaitForChild("\066\097\115\101\067\104\097\114\097\099\116\101\114\067\111\110\116\114\111\108\108\101\114"))local L=setmetatable({},U)L.__index=L function L.new()local H=setmetatable(U.new(),L)H.parentUIFrame=nil H.jumpButton=nil H.characterAddedConn=nil H.humanoidStateEnabledChangedConn=nil H.humanoidJumpPowerConn=nil H.humanoidParentConn=nil H.externallyEnabled=false H.jumpPower=0 H.jumpStateEnabled=true H.isJumping=false H.humanoid=nil return H end function L.EnableButton(L,U)if U then if not L.jumpButton then L:Create()end local U=Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if U and L.externallyEnabled then if L.externallyEnabled then if U.JumpPower>0 then L.jumpButton.Visible=true end end end else L.jumpButton.Visible=false L.isJumping=false L.jumpButton.ImageRectOffset=Vector2.new(1,146)end end function L.UpdateEnabled(U)if U.jumpPower>0 and U.jumpStateEnabled then U:EnableButton(true)else U:EnableButton(false)end end function L.HumanoidChanged(L,U)local H=Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if H then if U=="\074\117\109\112\080\111\119\101\114"then L.jumpPower=H.JumpPower L:UpdateEnabled()elseif U=="\080\097\114\101\110\116"then if not H.Parent then L.humanoidChangeConn:Disconnect()end end end end function L.HumanoidStateEnabledChanged(H,U,L)if U==Enum.HumanoidStateType.Jumping then H.jumpStateEnabled=L H:UpdateEnabled()end end function L.CharacterAdded(L,U)if L.humanoidChangeConn then L.humanoidChangeConn:Disconnect()L.humanoidChangeConn=nil end L.humanoid=U:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")while not L.humanoid do U.ChildAdded:wait()L.humanoid=U:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")end L.humanoidJumpPowerConn=(L.humanoid:GetPropertyChangedSignal("\074\117\109\112\080\111\119\101\114")):Connect(function()L.jumpPower=L.humanoid.JumpPower L:UpdateEnabled()end)L.humanoidParentConn=(L.humanoid:GetPropertyChangedSignal("\080\097\114\101\110\116")):Connect(function()if not L.humanoid.Parent then L.humanoidJumpPowerConn:Disconnect()L.humanoidJumpPowerConn=nil L.humanoidParentConn:Disconnect()L.humanoidParentConn=nil end end)L.humanoidStateEnabledChangedConn=L.humanoid.StateEnabledChanged:Connect(function(U,H)L:HumanoidStateEnabledChanged(U,H)end)L.jumpPower=L.humanoid.JumpPower L.jumpStateEnabled=L.humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)L:UpdateEnabled()end function L.SetupCharacterAddedFunction(U)U.characterAddedConn=Players.LocalPlayer.CharacterAdded:Connect(function(L)U:CharacterAdded(L)end)if Players.LocalPlayer.Character then U:CharacterAdded(Players.LocalPlayer.Character)end end function L.Enable(H,U,L)H.parentUIFrame=L H.externallyEnabled=U H:EnableButton(U)end function L.Create(U)if not U.parentUIFrame then return end if U.jumpButton then U.jumpButton:Destroy()U.jumpButton=nil end local L=math.min(U.parentUIFrame.AbsoluteSize.x,U.parentUIFrame.AbsoluteSize.y)local H=L<=500 local q=H and 70 or 120 U.jumpButton=Instance.new("\073\109\097\103\101\066\117\116\116\111\110")U.jumpButton.Name="\074\117\109\112\066\117\116\116\111\110"U.jumpButton.Visible=false U.jumpButton.BackgroundTransparency=1 U.jumpButton.Image=TOUCH_CONTROL_SHEET U.jumpButton.ImageRectOffset=Vector2.new(1,146)U.jumpButton.ImageRectSize=Vector2.new(144,144)U.jumpButton.Size=UDim2.new(0,q,0,q)U.jumpButton.Position=H and UDim2.new(1,-(q*1.5-10),1,-q-20)or UDim2.new(1,-(q*1.5-10),1,-q*1.75)local u=nil U.jumpButton.InputBegan:connect(function(L)if u or L.UserInputType~=Enum.UserInputType.Touch or L.UserInputState~=Enum.UserInputState.Begin then return end u=L U.jumpButton.ImageRectOffset=Vector2.new(146,146)U.isJumping=true end)local h=function()u=nil U.isJumping=false U.jumpButton.ImageRectOffset=Vector2.new(1,146)end U.jumpButton.InputEnded:connect(function(U)if U==u then h()end end)GuiService.MenuOpened:connect(function()if u then h()end end)if not U.characterAddedConn then U:SetupCharacterAddedFunction()end U.jumpButton.Parent=U.parentUIFrame end return L
--[[ The Module ]]
--
local BaseCharacterController = require(script.Parent:WaitForChild("BaseCharacterController"))
local TouchJump = setmetatable({}, BaseCharacterController)
TouchJump.__index = TouchJump

function TouchJump.new()
	local self = setmetatable(BaseCharacterController.new(), TouchJump)

	self.parentUIFrame = nil
	self.jumpButton = nil
	self.characterAddedConn = nil
	self.humanoidStateEnabledChangedConn = nil
	self.humanoidJumpPowerConn = nil
	self.humanoidParentConn = nil
	self.externallyEnabled = false
	self.jumpPower = 0
	self.jumpStateEnabled = true
	self.isJumping = false
	self.humanoid = nil -- saved reference because property change connections are made using it

	return self
end

function TouchJump:EnableButton(enable)
	if enable then
		if not self.jumpButton then
			self:Create()
		end
		local humanoid = Players.LocalPlayer.Character
			and Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
		if humanoid and self.externallyEnabled then
			if self.externallyEnabled then
				if humanoid.JumpPower > 0 then
					self.jumpButton.Visible = true
				end
			end
		end
	else
		self.jumpButton.Visible = false
		self.isJumping = false
		self.jumpButton.ImageRectOffset = Vector2.new(1, 146)
	end
end

function TouchJump:UpdateEnabled()
	if self.jumpPower > 0 and self.jumpStateEnabled then
		self:EnableButton(true)
	else
		self:EnableButton(false)
	end
end

function TouchJump:HumanoidChanged(prop)
	local humanoid = Players.LocalPlayer.Character and Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		if prop == "JumpPower" then
			self.jumpPower = humanoid.JumpPower
			self:UpdateEnabled()
		elseif prop == "Parent" then
			if not humanoid.Parent then
				self.humanoidChangeConn:Disconnect()
			end
		end
	end
end

function TouchJump:HumanoidStateEnabledChanged(state, isEnabled)
	if state == Enum.HumanoidStateType.Jumping then
		self.jumpStateEnabled = isEnabled
		self:UpdateEnabled()
	end
end

function TouchJump:CharacterAdded(char)
	if self.humanoidChangeConn then
		self.humanoidChangeConn:Disconnect()
		self.humanoidChangeConn = nil
	end

	self.humanoid = char:FindFirstChildOfClass("Humanoid")
	while not self.humanoid do
		char.ChildAdded:wait()
		self.humanoid = char:FindFirstChildOfClass("Humanoid")
	end

	self.humanoidJumpPowerConn = self.humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function()
		self.jumpPower = self.humanoid.JumpPower
		self:UpdateEnabled()
	end)

	self.humanoidParentConn = self.humanoid:GetPropertyChangedSignal("Parent"):Connect(function()
		if not self.humanoid.Parent then
			self.humanoidJumpPowerConn:Disconnect()
			self.humanoidJumpPowerConn = nil
			self.humanoidParentConn:Disconnect()
			self.humanoidParentConn = nil
		end
	end)

	self.humanoidStateEnabledChangedConn = self.humanoid.StateEnabledChanged:Connect(function(state, enabled)
		self:HumanoidStateEnabledChanged(state, enabled)
	end)

	self.jumpPower = self.humanoid.JumpPower
	self.jumpStateEnabled = self.humanoid:GetStateEnabled(Enum.HumanoidStateType.Jumping)
	self:UpdateEnabled()
end

function TouchJump:SetupCharacterAddedFunction()
	self.characterAddedConn = Players.LocalPlayer.CharacterAdded:Connect(function(char)
		self:CharacterAdded(char)
	end)
	if Players.LocalPlayer.Character then
		self:CharacterAdded(Players.LocalPlayer.Character)
	end
end

function TouchJump:Enable(enable, parentFrame)
	self.parentUIFrame = parentFrame
	self.externallyEnabled = enable
	self:EnableButton(enable)
end

function TouchJump:Create()
	if not self.parentUIFrame then
		return
	end

	if self.jumpButton then
		self.jumpButton:Destroy()
		self.jumpButton = nil
	end

	local minAxis = math.min(self.parentUIFrame.AbsoluteSize.x, self.parentUIFrame.AbsoluteSize.y)
	local isSmallScreen = minAxis <= 500
	local jumpButtonSize = isSmallScreen and 70 or 120

	self.jumpButton = Instance.new("ImageButton")
	self.jumpButton.Name = "JumpButton"
	self.jumpButton.Visible = false
	self.jumpButton.BackgroundTransparency = 1
	self.jumpButton.Image = TOUCH_CONTROL_SHEET
	self.jumpButton.ImageRectOffset = Vector2.new(1, 146)
	self.jumpButton.ImageRectSize = Vector2.new(144, 144)
	self.jumpButton.Size = UDim2.new(0, jumpButtonSize, 0, jumpButtonSize)

	self.jumpButton.Position = isSmallScreen and UDim2.new(1, -(jumpButtonSize * 1.5 - 10), 1, -jumpButtonSize - 20)
		or UDim2.new(1, -(jumpButtonSize * 1.5 - 10), 1, -jumpButtonSize * 1.75)

	local touchObject = nil
	self.jumpButton.InputBegan:connect(function(inputObject)
		--A touch that starts elsewhere on the screen will be sent to a frame's InputBegan event
		--if it moves over the frame. So we check that this is actually a new touch (inputObject.UserInputState ~= Enum.UserInputState.Begin)
		if
			touchObject
			or inputObject.UserInputType ~= Enum.UserInputType.Touch
			or inputObject.UserInputState ~= Enum.UserInputState.Begin
		then
			return
		end

		touchObject = inputObject
		self.jumpButton.ImageRectOffset = Vector2.new(146, 146)
		self.isJumping = true
	end)

	local OnInputEnded = function()
		touchObject = nil
		self.isJumping = false
		self.jumpButton.ImageRectOffset = Vector2.new(1, 146)
	end

	self.jumpButton.InputEnded:connect(function(inputObject)
		if inputObject == touchObject then
			OnInputEnded()
		end
	end)

	GuiService.MenuOpened:connect(function()
		if touchObject then
			OnInputEnded()
		end
	end)

	if not self.characterAddedConn then
		self:SetupCharacterAddedFunction()
	end

	self.jumpButton.Parent = self.parentUIFrame
end

return TouchJump
