local module = {}

function GiveCollarGetAttachment(player)
	local char = player.Character
	
	local g = script.Collar:clone()
	g.Parent = char
	local C = g:GetChildren()
	for i=1, #C do
		if C[i]:IsA("BasePart") then
			local W = Instance.new("Weld")
			W.Part0 = g.Middle
			W.Part1 = C[i]
			local CJ = CFrame.new(g.Middle.Position)
			local C0 = g.Middle.CFrame:inverse()*CJ
			local C1 = C[i].CFrame:inverse()*CJ
			W.C0 = C0
			W.C1 = C1
			W.Parent = g.Middle
		end
			local Y = Instance.new("Weld")
			Y.Part0 = char.Head
			Y.Part1 = g.Middle
			Y.C0 = CFrame.new(0, 0, 0)
			Y.Parent = Y.Part0
	end

	local h = g:GetChildren()
	for i = 1, # h do
		if h[i]:IsA("BasePart") then
			h[i].Anchored = false
			h[i].CanCollide = false
		end
	end
	
	return g.AttachmentPart.Attachment
end

function AddAttachmentToArm(player)
	local char = player.Character
	local newAttachment = Instance.new("Attachment")
	newAttachment.Position = Vector3.new(0,-1,0)
	newAttachment.Parent = char["Right Arm"]
	
	return newAttachment
end

function MakeLeash(Attachment0, Attachment1)
	local newRopeRenderer = script.RopeRenderer:Clone()
	newRopeRenderer.DrawLocalLine.Attachment0.Value = Attachment0
	newRopeRenderer.DrawLocalLine.Attachment1.Value = Attachment1
	newRopeRenderer.DrawLocalLine.RemoveEvent.Value = newRopeRenderer.RemoveCollar
	newRopeRenderer.Parent = workspace
	newRopeRenderer.Disabled = false
	
	return newRopeRenderer
end

function AddPlayerController(victim, owner, removeEvent)
	local newPlayerController = script.PlayerController:Clone()
	newPlayerController.Owner.Value = owner.Character
	newPlayerController.RemoveEvent.Value = removeEvent
	newPlayerController.Parent = victim.PlayerGui
	newPlayerController.Disabled = false
end

function AddCollarRemover(victim, owner, removeEvent)
	local newRemover = script.CollarRemover:Clone()
	newRemover.RemoveEvent.Value = removeEvent
	newRemover.Victim.Value = victim
	newRemover.Owner.Value = owner
	newRemover.Parent = workspace
	newRemover.Disabled = false
end

--function SwapToCollarAnimation(victim)
--	local character = victim.Character
--	local animationScript = character:WaitForChild("Animate")
--	animationScript.idle.Animation1.AnimationId = "rbxassetid://".. tostring(script.AnimationsSettings.IdleAnim.Value)
--	animationScript.idle.Animation2.Weight.Value = 0
--	
--	animationScript.jump.JumpAnim.AnimationId = "rbxassetid://".. tostring(script.AnimationsSettings.JumpAnim.Value)
--	animationScript.fall.FallAnim.AnimationId = "rbxassetid://".. tostring(script.AnimationsSettings.FallAnim.Value)
--	
--	animationScript.run.RunAnim.AnimationId = "rbxassetid://".. tostring(script.AnimationsSettings.CrawlAnim.Value)
--	animationScript.walk.WalkAnim.AnimationId = "rbxassetid://".. tostring(script.AnimationsSettings.CrawlAnim.Value)
--end

function module.MakeCollar(owner,victim)
	--crea collare ed attachment
	local attachment0 = GiveCollarGetAttachment(victim)
	local attachment1 = AddAttachmentToArm(owner)
	--
	
	--crea leash
	local newRope = MakeLeash(attachment0, attachment1)
	--
	
	--aggiungi il controllo del giocatore
	AddPlayerController(victim, owner, newRope.RemoveCollar)
	--
	
	--finisci con il controllo della rimozione
	AddCollarRemover(victim,owner,newRope.RemoveCollar)
	--
	
	--cambia le animazioni
	--SwapToCollarAnimation(victim)
	--
end

return module

turn this module into a serverscript and remove all comments. also use local.