Menu
CoddyTech

업그레이드 기능

Coddy의 Roblox 게임: 팩토리 타이쿤 코스 레슨. 22개 중 11번째.

벨트 위의 보라색 문은 업그레이더입니다. 광석을 막지는 않지만(CanCollide가 아니므로), 이 문을 통과하는 모든 광석의 가치는 두 배가 됩니다.

같은 광석에 대해 Touched가 두 번 이상 실행될 수 있습니다. 예를 들어 광석이 멈췄다가 다시 움직이는 경우가 그렇습니다. 두 배로 만드는 작업이 두 번 이루어지면 플레이어는 좋아하고 소유자는 싫어하는 버그가 발생합니다. 따라서 업그레이더는 두 배로 만든 각 광석에 속성을 표시하고, 표시된 광석은 건너뜁니다.

if hit.Name == "Ore" and not hit:GetAttribute("Upgraded") then
	hit:SetAttribute("Upgraded", true)
	hit:SetAttribute("Value", hit:GetAttribute("Value") * 2)
end

업그레이더는 구매될 때까지 ServerStorage에서 대기하지만, Touched는 시작할 때 연결할 수 있습니다. 연결은 파트가 어디에 있든 파트에 유지되며, Touched는 파트가 월드에 들어온 뒤에만 실행됩니다.

challenge icon

챌린지

중급

보라색 업그레이더를 통과하는 모든 광석의 값을 두 배로 만드세요.

  1. items.Upgrader.Touched를 연결하고, 이름이 Ore이고 Upgraded 특성이 없는 파트에만 작동하게 하세요.
  2. Upgraded를 true로 설정하고, Value 특성 값을 두 배로 만든 다음 색상을 Color3.fromRGB(155, 89, 182)로 설정하세요.

‘플레이’를 누르고 업그레이더를 구매하세요. 광석은 통과하면서 보라색으로 변하고 20을 지급합니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

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

local tycoon = workspace.Tycoon
local items = ServerStorage.TycoonItems
local owner = nil

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)

-- 공장 위의 표지판
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: dropper 아래에서 떨어지는 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)
	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

-- 컨베이어는 위에 놓인 물체를 운반합니다
tycoon.Conveyor.AssemblyLinearVelocity = Vector3.new(Settings.BeltSpeed, 0, 0)

-- 수집기는 광석을 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)

-- 레슨 11: Upgrader가 들어갈 자리

-- 구매한 아이템을 공장에 배치합니다
local function build(item)
	item.Parent = tycoon.Built
	if item:HasTag("Dropper") then
		startDropper(item)
	end
end

-- 구매: 발판이 사라지고 해당 아이템이 설치됩니다
local function buy(pad, player)
	if player ~= owner or pad.Parent ~= tycoon.Buttons then
		return
	end
	local cash = player.leaderstats.Cash
	local price = pad:GetAttribute("Price")
	if cash.Value < price then
		return
	end
	cash.Value -= price
	pad.Parent = ServerStorage
	build(items:FindFirstChild(pad:GetAttribute("Item")))
end

for _, pad in tycoon.Buttons:GetChildren() do
	pad.Touched:Connect(function(hit)
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			buy(pad, player)
		end
	end)
end

-- 모든 발판 위에 가격표가 떠 있습니다
for _, pad in tycoon.Buttons:GetChildren() do
	local tag = Instance.new("BillboardGui")
	tag.Name = "PriceTag"
	tag.Size = UDim2.fromOffset(160, 40)
	tag.StudsOffset = Vector3.new(0, 3, 0)
	tag.Parent = pad

	local label = Instance.new("TextLabel")
	label.Size = UDim2.fromScale(1, 1)
	label.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
	label.BackgroundTransparency = 0.2
	label.TextColor3 = Color3.fromRGB(255, 255, 255)
	label.TextScaled = true
	label.Text = pad:GetAttribute("Label") .. ": $" .. pad:GetAttribute("Price")
	label.Parent = tag
end

-- 소유권 획득: 발판을 처음 밟은 플레이어가 공장의 주인이 됩니다
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)

Roblox 게임: 팩토리 타이쿤의 모든 레슨

직접 연습해 보세요: Roblox 플레이그라운드