Menu
CoddyTech

תפוס בעלות על המפעל

שיעור 6 מתוך 18 בקורס משחק Roblox: טייקון מפעל של Coddy.

טייקון שייך למישהו. המשטח הירוק ליד הכניסה תובע בעלות עליו: השחקן הראשון שעולה עליו הופך לבעלים, המפעל מתחיל לפעול, ורק הבעלים מקבל תשלום. הבעלים מיוצג באמצעות משתנה אחד בראש הסקריפט, שערכו nil עד שמישהו תובע בעלות:

local owner = nil

claimPad.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player and not owner then
		owner = player
	end
end)

כשתובעים בעלות על המפעל, כל המתקנים להטלת פריטים שבו מתחילים לפעול. למתקנים להטלת פריטים יש תג, Dropper, ולכן CollectionService:GetTagged("Dropper") מוצא את כולם, בכל מקום שבו הם נמצאים. מתקנים להטלת פריטים שנרכשו ימתינו ב-ServerStorage, כך שרק אלה שב-workspace יתחילו לפעול.

בהמשך אפשר לתבוע שוב בעלות על המפעל אחרי שבעליו עוזב. כדי שאף מתקן להטלת פריטים לא יפעיל שתי לולאות, startDropper מסמן מתקן שפועל באמצעות מאפיין, ולא עושה דבר אם המאפיין מוגדר.

שלט מעל המפעל מציין למי הוא שייך: SurfaceGui על tycoon.Sign, כמו בכל שלט.

challenge icon

אתגר

בינוני

הפוך את המפעל לזמין לתביעת בעלות:

  1. קבל את CollectionService, והוסף local owner = nil סמוך לראש הקוד.
  2. הצב שלט על tycoon.Sign: ‏SurfaceGui ובתוכו TextLabel בשם Status שממלא אותו, עם טקסט לבן ומוגדל על רקע כהה, המציג את Step on the green pad to claim this factory, ופונקציה setSign(text).
  3. בתוך startDropper, החזר מיד אם המאפיין Running של המתקן מוגדר; אחרת, הגדר אותו ונקה אותו כשהלולאה מסתיימת.
  4. הסר את השורה שמפעילה את tycoon.Dropper. כששחקן דורך על tycoon.ClaimPad ואין בעלים: הגדר אותו כבעלים, הפוך את המשטח לאפור (Color3.fromRGB(120, 120, 120), ‏SmoothPlastic), הגדר את השלט ל-Player1's Factory (עם ה-Name שלו), והפעל כל מתקן שמסומן בתג ונמצא ב-workspace.
  5. שלם רק לבעלים בקולט (אם יש כזה).

לחץ על Play ותבע בעלות על המפעל שלך, ואז לחץ על Check.

נסו בעצמכם

local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local Settings = require(game.ReplicatedStorage.Settings)

local tycoon = workspace.Tycoon

print("Ore is worth " .. Settings.OreValue .. " Cash")

-- כל שחקן מקבל Cash בטבלת המובילים
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
end)

-- Ore: חלק קטן ששווה Cash, שנופל מתחת ל-dropper
local function dropOre(dropper)
	local ore = Instance.new("Part")
	ore.Name = "Ore"
	ore.Size = Vector3.new(1, 1, 1)
	ore.Color = Color3.fromRGB(150, 150, 160)
	ore.Position = dropper.Position - Vector3.new(0, 2.5, 0)
	ore:SetAttribute("Value", Settings.OreValue)
	if math.random() < Settings.GoldChance then
		ore.Color = Color3.fromRGB(255, 200, 40)
		ore.Material = Enum.Material.Neon
		ore:SetAttribute("Value", Settings.OreValue * 5)
	end
	ore.Parent = workspace
	Debris:AddItem(ore, 20)
end

local function startDropper(dropper)
	task.spawn(function()
		while dropper:IsDescendantOf(workspace) do
			dropOre(dropper)
			task.wait(Settings.DropInterval)
		end
	end)
end

startDropper(tycoon.Dropper)

-- המסוע מעביר את כל מה שנמצא עליו
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)

-- האספן הופך Ore ל-Cash
tycoon.Collector.Touched:Connect(function(hit)
	if hit.Name == "Ore" then
		local value = hit:GetAttribute("Value")
		hit:Destroy()
		for _, player in Players:GetPlayers() do
			player.leaderstats.Cash.Value += value
		end
	end
end)
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה משחק Roblox: טייקון מפעל