Help with Tycoon system

I currently am making my own tycoon system with a button that has multiple choices. You will eventually be able to sell it then buy a different choice, but that is for later.

I need help, as I am making a table then putting items in it.

Here is my current script:

Whole script
local Objects = {}

local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(0.75,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)

local function Sound(id)

end

local function MakeChoice(plr, name, price, button)
	local object = Objects[name]
	if object then
		if plr.leaderstats.Cash.Value >= price then
			object.Parent = script.Parent.PurchasedItems
			plr.leaderstats.Cash.Value -= price
			
			game.ReplicatedStorage.Remotes.Events.CloseGUI:FireClient(plr, "ChooseButton")
			
			button.Button.CanCollide = false
			button.Button:FindFirstChild("ButtonBillboard").Enabled = false
			TweenService:Create(button.Button, info, {Transparency = 1}):Play()
		end
	end
end

for i, object in pairs(script.Parent.Purchases:GetChildren()) do
	Objects[object.Name] = object:Clone()
	object:Destroy()
end

for i, button in pairs(script.Parent.Buttons:GetDescendants()) do
	if button:FindFirstChild("Button") then
		local billboard = game.ServerStorage.ButtonBillboard:Clone()
		billboard.Parent = button.Button
		billboard.TextLabel.Text = button.Name
	end
end

for i, button in pairs(script.Parent.Buttons.Create:GetChildren()) do
	if button:FindFirstChild("Button") then
		if not Objects[button.Object.Value] then
			button.Button.CanCollide = false
			button.Button.Transparency = 1
			button.Button:FindFirstChild("ButtonBillboard").Enabled = false
		end

		if button:FindFirstChild("Dependency") then
			button.Button.CanCollide = false
			button.Button.Transparency = 1
			button.Button:FindFirstChild("ButtonBillboard").Enabled = false
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedItems:WaitForChild(button.Dependency.Value) then
					print("object in folder")
					TweenService:Create(button.Button, info, {Transparency = 0}):Play()
					button.Button.CanCollide = true
					button.Button.Transparency = 0
					button.Button:FindFirstChild("ButtonBillboard").Enabled = true
				end
			end))
		end

		button.Button.Touched:Connect(function(hit)
			if button.Button.CanCollide == true then
				if hit.Parent:FindFirstChild("Humanoid") then
					--print("character")
					local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
					if plr then
						--print("player")
						if script.Parent.Owner.Value == plr then
							--print("owner is player")
							local cash = plr.leaderstats.Cash
							if cash.Value >= button.Price.Value then
								--print('enough cash')
								--Sound()
								Objects[button.Object.Value].Parent = script.Parent.PurchasedItems
								--print("object in folder")
								cash.Value -= button.Price.Value
								button.Button.CanCollide = false
								button.Button:FindFirstChild("ButtonBillboard").Enabled = false
								TweenService:Create(button.Button, info, {Transparency = 1}):Play()
							end
						end
					end
				end
			end
		end)
	end
end

for i, button in pairs(script.Parent.Buttons.Destroyed:GetChildren()) do
	if button:FindFirstChild("Button") then
		if not Objects[button.Destroyed.Value] and not Objects[button.Object.Value] then
			button.Button.CanCollide = false
			button.Button.Transparency = 1
			button.Button:FindFirstChild("ButtonBillboard").Enabled = false
		end

		if button:FindFirstChild("Dependency") then
			button.Button.CanCollide = false
			button.Button.Transparency = 1
			button.Button:FindFirstChild("ButtonBillboard").Enabled = false
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedItems:WaitForChild(button.Dependency.Value) then
					print("object in folder")
					TweenService:Create(button.Button, info, {Transparency = 0}):Play()
					button.Button.CanCollide = true
					button.Button.Transparency = 0
					button.Button:FindFirstChild("ButtonBillboard").Enabled = true
				end
			end))
		end

		button.Button.Touched:Connect(function(hit)
			if button.Button.CanCollide == true then
				if hit.Parent:FindFirstChild("Humanoid") then
					--print("character")
					local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
					if plr then
						--print("player")
						if script.Parent.Owner.Value == plr then
							--print("owner is player")
							local cash = plr.leaderstats.Cash
							if cash.Value >= button.Price.Value then
								--print('enough cash')
								--Sound()
								script.Parent.PurchasedItems[button.Destroyed.Value]:Destroy()
								Objects[button.Object.Value].Parent = script.Parent.PurchasedItems
								--print("object in folder")
								cash.Value -= button.Price.Value
								button.Button.CanCollide = false
								button.Button:FindFirstChild("ButtonBillboard").Enabled = false
								TweenService:Create(button.Button, info, {Transparency = 1}):Play()
							end
						end
					end
				end
			end
		end)
	end
end

for i, button in pairs(script.Parent.Buttons.Choice:GetChildren()) do
	if button:FindFirstChild("Button") then
		for _, item in pairs(button.Choices:GetChildren()) do
			if not Objects[item.Name] then
				button.Button.CanCollide = false
				button.Button.Transparency = 1
				button.Button:FindFirstChild("ButtonBillboard").Enabled = false
				
				break
			end
		end

		if button:FindFirstChild("Dependency") then
			button.Button.CanCollide = false
			button.Button.Transparency = 1
			button.Button:FindFirstChild("ButtonBillboard").Enabled = false
			coroutine.resume(coroutine.create(function()
				if script.Parent.PurchasedItems:WaitForChild(button.Dependency.Value) then
					print("object in folder")
					TweenService:Create(button.Button, info, {Transparency = 0}):Play()
					button.Button.CanCollide = true
					button.Button.Transparency = 0
					button.Button:FindFirstChild("ButtonBillboard").Enabled = true
				end
			end))
		end

		button.Button.Touched:Connect(function(hit)
			if button.Button.CanCollide == true then
				if hit.Parent:FindFirstChild("Humanoid") then
					--print("character")
					local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
					if plr then
						local choices = {}
						for index, CHOICE in pairs(button.Choices:GetChildren()) do
							print("ddDddddddddddddddd")
							choices[CHOICE.Name] = choices[CHOICE.Value] --Name = Price
							print(CHOICE.Name, CHOICE.Value)
							print(choices[CHOICE.Name])
						end
						print(choices)
						game.ReplicatedStorage.Remotes.Events.ChoiceMenu:FireClient(plr, choices, button)
					end
				end
			end
		end)
	end
end

game.ReplicatedStorage.Remotes.Events.Choose.OnServerEvent:Connect(MakeChoice)
Need help on
local choices = {}
						for index, CHOICE in pairs(button.Choices:GetChildren()) do
							print("ddDddddddddddddddd")
							choices[CHOICE.Name] = choices[CHOICE.Value] --Name = Price
							print(CHOICE.Name, CHOICE.Value)
							print(choices[CHOICE.Name])
						end
						print(choices)
						game.ReplicatedStorage.Remotes.Events.ChoiceMenu:FireClient(plr, choices, button)

If there is a way to lessen the lines of code in the script, that would be useful.

My output shows:

Choice1 100
nil

and

Choice2 150
nil

That means choices[CHOICE.Name] is nil, when it shouldn’t be.

All help is appreciated, thanks :slight_smile:

1 Like

When you set the value in choices[CHOICE.Name] = choices[CHOICE.Value], it should be choices[CHOICE.Name] = CHOICE.Value.

By doing choices[CHOICE.Value], you’re going into the dictionary and searching for the value of “CHOICE” which is nil.

1 Like