Tycoon buy function not working?!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make tycoon buy function with button.

  2. 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.

  3. 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

Thanks.

1 Like

Did you define Players?

Server

-- 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.

game:GetService("Players")

You can also say

game.Players
1 Like

I got variable of Player above

local Players = game:GetService("Players")

Btw If I put a print function above the Purchase line, it doesn’t print.

1 Like

Oh, alright.

What is Owner? Is it a stringvalue? Object?

1 Like

What is Owner? Owner is String Value

I think the problem is the newButton spawn into the folder and the for loop didn’t detects it.

Did you put a print above this?

I would say print under this line the NextButton to see if it even shows one

Maybe you are right and it is that

yes, I use put that for testing to see if the for loop detect the newButton so it can detect touch event to do purchase

print("Test")
ButtonModule.Purchase(player, button, Tycoon)

And yet, still didn’t print anything.

Question, have you printed nextbutton to see if it’s nil?

So, How can I fix this problem? Or should I change the logic

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?

The last button didn’t print nil, so it means that the touch event didn’t work

Try printing in the touched event. Is the player coming back as nil?

ButtonStorage is a folder inside the ReplicatedStorage

Are you adding to that folder?

Um yeah. Is there a better way to store the buttons that are haven’t unlocked or already unlocked and the items that are haven’t unlocked

Didn’t print anything. The touch event didn’t fire.

Is the button anchored and in the workspace?