You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make tycoon buy function with button.
What is the issue? Include screenshots / videos if possible!
I use for loop for button folder to make touch event work. But when the item is bought, the button spawn in new to the button folder and the touch event doesn’t work anymore.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I am trying to find solution in Devforum!
-- Tycoon Server Script
for _, button in pairs(Buttons:GetChildren()) do
button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player.Name == Owner.Value then
ButtonModule.Purchase(player, button, Tycoon)
end
end
end)
end
-- Button modules function
function button.Purchase(player, button, tycoon)
local leaderstats = player:WaitForChild("leaderstats")
local Cash = leaderstats:WaitForChild("Cash")
local Cost = button.Cost
local Item = ItemStorage:WaitForChild(button.Item.Value)
local NextButton
if button:FindFirstChild("NextButton") then
NextButton = ButtonStorage:FindFirstChild(button.NextButton.Value)
else
NextButton = nil
end
if Cash.Value >= Cost.Value then
button.Parent = ButtonStorage
Item.Parent = tycoon.Items
NextButton.Parent = tycoon.Buttons
end
end
-- Tycoon Server Script
for _, button in pairs(Buttons:GetChildren()) do
button.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.Name == Owner.Value then
ButtonModule.Purchase(player, button, Tycoon)
end
end
end)
end
Players is a service. I don’t think you can just say Players.
Well, if you are adding new buttons with a function. You would need to tell the script that. So, maybe try putting the ButtonStorage variable in the function?