﻿     1→-- Craft House: crafted plot items made from toys (Config.CRAFTS).
     2→-- RemoteFunction "Crafting": Craft(id) -> { ok, msg, used = { { Toy, Rarity, Variant }, ... } }
     3→-- The server picks the toys itself (Config.CraftPick: cheapest fitting toys, never ones on the tree), takes them and
     4→-- adds the item (PlayerData.CraftItem). The item can then be placed on the plot (Shop "Place") or sold on the market.
     5→local Players = game:GetService("Players")
     6→local ReplicatedStorage = game:GetService("ReplicatedStorage")
     7→local ServerStorage = game:GetService("ServerStorage")
     8→
     9→local folder = ReplicatedStorage:WaitForChild("OrnamentMachine")
    10→local Config = require(folder:WaitForChild("Config"))
    11→local PlayerData = require(script.Parent:WaitForChild("OrnamentPlayerData"))
    12→local craftFn = folder:WaitForChild("Crafting")
    13→local notify = folder:WaitForChild("Notify")
    14→
    15→local last = {}
    16→craftFn.OnServerInvoke = function(player, action, id)
    17→	local now = os.clock()
    18→	if last[player] and now - last[player] < 0.5 then return { ok = false, msg = "Too fast" } end
    19→	last[player] = now
    20→	if not player:GetAttribute("DataLoaded") then return { ok = false, msg = "Your data is still loading" } end
    21→	if action ~= "Craft" or type(id) ~= "string" or not Config.CraftById[id] then return { ok = false } end
    22→	local c = Config.CraftById[id]
    23→	local ok, taken = PlayerData.CraftItem(player, id)
    24→	if not ok then return { ok = false, msg = taken } end
    25→	if c.Xp and c.Xp > 0 then
    26→		local giveXP = ServerStorage:FindFirstChild("OrnamentGiveXP")
    27→		if giveXP then giveXP:Fire(player, c.Xp) end
    28→	end
    29→	task.spawn(PlayerData.Save, player)
    30→	local used = {}
    31→	for _, it in taken do table.insert(used, { Toy = it.Toy, Rarity = it.Rarity, Variant = it.Variant }) end
    32→	return { ok = true, msg = c.Name .. " crafted! It's in your inventory", used = used }
    33→end
    34→Players.PlayerRemoving:Connect(function(p) last[p] = nil end)
    35→
