--- VARIABLES/CONSTANTS ---


THROW_MULTIPLIER = 0.25




local Tool = {}

Tool.MouseButton1 = function(Character, ExtraData) -- Throw
	local MousePosition = ExtraData[1]
	local Net = Character:FindFirstChildOfClass("Tool")
	
	if not Net:FindFirstChild("Display") then return end
	
	
	Character.Humanoid:LoadAnimation(script.Toss):Play()
	
	task.wait(0.3)
	
	local Model = Instance.new("Model", workspace.Debris)
	Model.Name = Character.Name.."_"..script.Name
	local CloneNet = Net:Clone()
	CloneNet.Display.Parent = Model
	CloneNet.Handle.Parent = Model
	CloneNet:Destroy()
	
	local Display = Net.Display
	Display.Parent = ServerStorage.PlayerStorage
	
	-- Throw Model.Handle
	local throwForce = 100*THROW_MULTIPLIER -- Adjust this value to change the throwing force

	local Handle = Model.Handle -- Assuming the net's handle is named "Handle"
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Velocity = (MousePosition - Handle.Position).unit * throwForce + Vector3.new(0,50,0)
	BodyVelocity.P = math.huge -- Sets linear damping to zero so the net doesn't slow down over time
	BodyVelocity.Parent = Handle
	
	local Connection
	-- Connect touch function, trip enemy
	Connection = Handle.Touched:Connect(function(hit)
		if hit:IsDescendantOf(workspace.Enemies) and hit.Parent:FindFirstChild("Humanoid") then
			local VictimCharacter = hit.Parent
			local Humanoid = Character:FindFirstChildOfClass("Humanoid")
			
			Connection:Disconnect()
			
			Ragdoll.Toggle(VictimCharacter)
			
			Model:Destroy()
			
			-- Clone ServerStorage.Models.NetDisplay above the enemy to simulate the net falling on top of it, use its handle to cframe it
				-- This will be the visual representation of the net trapping the enemy
			local NetDisplay = ServerStorage.Models.NetDisplay:Clone()
			NetDisplay.Parent = workspace.Debris
			NetDisplay:MoveTo((VictimCharacter.HumanoidRootPart.CFrame * CFrame.new(0,5,0)).Position)
			
			--game.Debris:AddItem(NetDisplay, 10)
			
		end
	end)

	-- Optionally, you can remove the BodyVelocity after a certain amount of time
	local removeDelay = .5 -- Adjust this value to change the delay before removing the BodyVelocity
	wait(removeDelay)
	BodyVelocity:Destroy()
	
	task.wait(5)

	Display.Parent = Net
	
end
Tool.Z = function(Character)
	print("hai Z")
end
Tool.X = function(Character)
	print("hai X")
end	



return Tool
