--- Drops the bag full of money onto the ground.
---@param Player Player
---@param Amount number

local function Handler(Player: Player, Amount : number)
	local Character: Model = Player.Character

	--// Sanity checks
	if (Player.Data.Silver.Value <= Amount) then
		return
	end
	--// Takes away the cash you just dropped from your data
	Player.Data.Silver.Value -= Amount
	
	--// I split a lot of the components of this up because it *sucks* looking at a huge wall of property changes, this makes it a bit easier.
	local Dropped: BasePart = script.ItemTemplate:Clone()

	Dropped.Name = Player.Name.."'s Dropped Silver"
	Dropped.BillboardGui.Enabled = true
	Dropped.BillboardGui.ToolName.Text = Amount

	Dropped.CFrame = Character.HumanoidRootPart.CFrame * CFrame.new(0,-2,-3)
	Dropped.Parent = workspace.DroppedBags

	local HasHit = false
	local Connection; --// Moved down here because I literally thought this was a global at first.
	Connection = Dropped.Touched:Connect(function(Hit)
		if (HasHit) then return end

		if (IsCharacter(Hit.Parent)) then
			HasHit = GiveSilver(Amount, Hit.Parent); --// We know GiveSilver returns a boolean, so we can just set 'HasHit' to it.

			if (HasHit) then
				Dropped:Destroy()
				Connection:Disconnect()
			end
		end
	end)
end

Event.OnServerEvent:Connect(Handler)
