Un pad di acquisto
Lezione 7 di 18 del corso Gioco Roblox: Tycoon della fabbrica di Coddy.
I contanti si usano per fare acquisti. I pad blu davanti al nastro acquistano ciascuno qualcosa; il primo acquista un secondo droppatore. Un pad conosce il proprio prezzo grazie a un attributo, Price, impostato durante la creazione del mondo.
L’oggetto che acquista aspetta in ServerStorage.TycoonItems, già al posto giusto. L’acquisto lo sposta nella cartella Built della fabbrica, che lo inserisce nel mondo, e sposta il pad in ServerStorage, dove nessuno può calpestarlo. Spostato, non distrutto: una lezione successiva riporterà i pad.
if cash.Value >= price then
cash.Value -= price
pad.Parent = ServerStorage
items.Dropper2.Parent = tycoon.Built
endOgni arto che tocca il pad attiva Touched, alcune volte nello stesso istante. Dopo il primo acquisto, il pad non è più in tycoon.Buttons, quindi controllarlo per primo impedisce un doppio acquisto.
Sfida
MedioFai funzionare la prima piattaforma:
- Ottieni
ServerStoragee tienilocal items = ServerStorage.TycoonItemsvicino all'inizio. - Quando un giocatore tocca
tycoon.Buttons.Dropper2Pad: non fare nulla a meno che non sia il proprietario e la piattaforma sia ancora intycoon.Buttons. Se il suo Cash è almeno pari all'attributoPricedella piattaforma, sottrai il prezzo, sposta la piattaforma in ServerStorage, spostaitems.Dropper2intycoon.Builte avviala.
Premi Gioca, guadagna 30 Cash e acquistala, poi premi Verifica.
Provalo tu
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")
local Settings = require(game.ReplicatedStorage.Settings)
local tycoon = workspace.Tycoon
local owner = nil
print("Ore is worth " .. Settings.OreValue .. " Cash")
-- Ogni giocatore riceve Cash nella classifica
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)
-- Il cartello sopra la fabbrica
local gui = Instance.new("SurfaceGui")
gui.Parent = tycoon.Sign
local signLabel = Instance.new("TextLabel")
signLabel.Name = "Status"
signLabel.Size = UDim2.fromScale(1, 1)
signLabel.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
signLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
signLabel.TextScaled = true
signLabel.Text = "Step on the green pad to claim this factory"
signLabel.Parent = gui
local function setSign(text)
signLabel.Text = text
end
-- Ore: un piccolo oggetto che vale Cash, lasciato cadere sotto un 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
-- Un dropper esegue un solo ciclo, indipendentemente da quante volte viene avviato
local function startDropper(dropper)
if dropper:GetAttribute("Running") then
return
end
dropper:SetAttribute("Running", true)
task.spawn(function()
while dropper:IsDescendantOf(workspace) do
dropOre(dropper)
task.wait(Settings.DropInterval)
end
dropper:SetAttribute("Running", nil)
end)
end
-- Il nastro trasportatore trasporta tutto ciò che vi si trova sopra
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)
-- Il raccoglitore converte Ore in Cash
tycoon.Collector.Touched:Connect(function(hit)
if hit.Name == "Ore" then
local value = hit:GetAttribute("Value")
hit:Destroy()
if owner then
owner.leaderstats.Cash.Value += value
end
end
end)
-- Acquisizione: il primo giocatore che sale sulla piattaforma diventa proprietario della fabbrica
local claimPad = tycoon.ClaimPad
claimPad.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and not owner then
owner = player
claimPad.Color = Color3.fromRGB(120, 120, 120)
claimPad.Material = Enum.Material.SmoothPlastic
setSign(player.Name .. "'s Factory")
for _, dropper in CollectionService:GetTagged("Dropper") do
if dropper:IsDescendantOf(workspace) then
startDropper(dropper)
end
end
end
end)
Questa lezione include un breve quiz. Inizia la lezione per rispondere e tenere traccia dei tuoi progressi.
Tutte le lezioni di Gioco Roblox: Tycoon della fabbrica
4Trasformalo in un gioco
Fabbrica completata