local objects = {} local teamcolor = BrickColor.new(script.Parent.Name) local…
- User
local objects = {} local teamcolor = BrickColor.new(script.Parent.Name) local TycoonModel = script.Parent
TycoonModel:WaitForChild("Buttons") TycoonModel:WaitForChild("Purchases")
local function getPlayerFromHit(hit) return game.Players:GetPlayerFromCharacter(hit.Parent) end
local function playerOwnsTycoon(player) return player and TycoonModel.Owner.Value == player end
local function characterIsAlive(character) local humanoid = character:FindFirstChild("Humanoid") return humanoid and humanoid.Health > 0 end
local function getCashStorage(player) return game.ServerStorage.MoneyStorage:FindFirstChild(player.Name) end
TycoonModel.Essentials.Collector.Touched:Connect(function(hit) local cash = hit:FindFirstChild("Cash") if cash then TycoonModel.Cash.Value += cash.Value game.Debris:AddItem(hit, 0) end end)
TycoonModel.Essentials.Giver.Touched:Connect(function(hit) local player = getPlayerFromHit(hit) if player and playerOwnsTycoon(player) and characterIsAlive(hit.Parent) then local cashStorage = getCashStorage(player) if cashStorage then cashStorage.Value += TycoonModel.Cash.Value TycoonModel.Cash.Value = 0 end end end)
for _, button in TycoonModel.Buttons:GetChildren() do local head = button:FindFirstChild("Head") if head then local objectName = button.Object and button.Object.Value local purchaseObject = objectName and TycoonModel.Purchases:FindFirstChild(objectName) if purchaseObject then objects[objectName] = purchaseObject:Clone() purchaseObject:Destroy() else warn("Button: " .. button.Name .. " is missing its object and has been removed.") head.CanCollide = false head.Transparency = 1 end
codeif button:FindFirstChild("Dependency") then head.CanCollide = false head.Transparency = 1 coroutine.wrap(function() while not TycoonModel.PurchasedObjects:FindFirstChild(button.Dependency.Value) do task.wait() end head.CanCollide = true head.Transparency = 0 end)() end head.Touched:Connect(function(hit) local player = getPlayerFromHit(hit) if player and head.CanCollide and playerOwnsTycoon(player) and characterIsAlive(hit.Parent) then local cashStorage = getCashStorage(player) if cashStorage and cashStorage.Value >= button.Price.Value then cashStorage.Value -= button.Price.Value head.Transparency = 1 head.CanCollide = false objects[objectName].Parent = TycoonModel.PurchasedObjects end end end) end - Assistant
- User
what is the purpose of the last line of code?
- Assistant
The purpose of the last line of code:
luaTycoonsFolder.ChildAdded:Connect(setupTycoon)is to handle dynamic addition of new Tycoons to the
workspace.Tycoonsfolder.Detailed Explanation
-
ChildAdded Event:
- This is an event provided by the
Instanceclass in Roblox. It triggers whenever a new child is added to an instance.
- This is an event provided by the
-
:
-
- User
local purchaseObject = objectName and tycoonModel.Purchases:FindFirstChild(objectName) if purchaseObject then objects[objectName] = purchaseObject:Clone() purchaseObject:Destroy() else
the destroy makes other codes error.
- Assistant
- User
for _, button in tycoonModel.Buttons:GetChildren() do local head = button:FindFirstChild("Head") if head then local objectName = button.Object and button.Object.Value local purchaseObject = objectName and tycoonModel.Purchases:FindFirstChild(objectName) if purchaseObject then objects[objectName] = purchaseObject:Clone() local TycoonSSFolder = Instance.new("Folder") TycoonSSFolder.Name = tycoonModel.Name TycoonSSFolder.Parent = game.ServerStorage.Assets.Tycoons
there's 9 folders created instead of one.
- Assistant