האספן
שיעור 4 מתוך 18 בקורס משחק Roblox: טייקון מפעל של Coddy.
בקצה המסוע, הבלוק הירוק הזוהר הוא האספן. כשעפרה נוגעת בו, העפרה מומרת למזומן: האספן קורא את הערך של העפרה, משמיד את העפרה ומשלם.
collector.Touched:Connect(function(hit)
if hit.Name == "Ore" then
local value = hit:GetAttribute("Value")
hit:Destroy()
-- שלם את הערך
end
end)בדקו קודם את השם: גם שחקנים וכל דבר אחר יכולים לגעת באספן. קראו את הערך לפני השמדת העפרה. כרגע האספן משלם לכל שחקן במשחק; בפרק הבא רק הבעלים של המפעל יקבל תשלום.
אתגר
קלחבר את tycoon.Collector.Touched: כאשר החלק שנגע בו נקרא Ore, קרא את המאפיין Value שלו, השמד אותו והוסף את הערך למזומנים של כל שחקן.
לחץ על Play וצפה במזומנים שלך מצטברים, ואז לחץ על Check.
נסו בעצמכם
local Players = game:GetService("Players")
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 ונופל מתחת למתקן ההפלה
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)
ore.Parent = workspace
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)
השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.