Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Am making a tycoon gives me nil values error?

So, i am making a new tycoon where you have a gui and but stuffs not walk on buttons and i cant get this to work it gives me an nil value error and i dont know why heres the error

17:29:22.854 - Players.ObStactionD3stroy3r.PlayerGui.tycoonUI.frameTycoon.storeFolder.storeTycoon.base_item.buttonPurchase.buttonDriverTycoon:22: attempt to index global 'tycoon' (a nil value)
17:29:22.854 - Stack Begin
17:29:22.855 - Script 'Players.ObStactionD3stroy3r.PlayerGui.tycoonUI.frameTycoon.storeFolder.storeTycoon.base_item.buttonPurchase.buttonDriverTycoon', Line 22 - global onClick
17:29:22.855 - Script 'Players.ObStactionD3stroy3r.PlayerGui.tycoonUI.frameTycoon.storeFolder.storeTycoon.base_item.buttonPurchase.buttonDriverTycoon', Line 52

And heres the script.



wait(0.5)
item = script.Parent.Parent
itemType = item.itemType.Value
itemCost = item.itemCost.Value
itemID = item.itemID.Value
itemLocation = tostring(item.itemLocation.Value)
itemName = tostring(item.itemName.Value)
script.Parent.Parent.textName.Text = tostring(itemName).. ' | '.. tostring(itemCost).. ' Cash'

function onClick()
	player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
	
	cash = player.stats.Cash.Value

	tycoon = game.Workspace:FindFirstChild("tycoon_".. player.Name)
	
	print("type 1")
	if itemType == 1 then
		if cash >= itemCost then
			itemGrab = tycoon.tycoonPurchases:FindFirstChild(itemLocation)
			itemGrab.isPurchased.Value = true
			
			script.Parent.Parent.isItemPurchased.Value = true
		else
			script.Parent.Text = "Not Enough Funds"
			wait(3)
			script.Parent.Text = 'Purchase'	
	end
	elseif itemtype == 2 then
		
		
		
	elseif itemtype == 3 then
		
		
		
	end
end

function isPurchased()
if script.Parent.Parent.isItemPurchased.Value == true then
	script.Parent.Text = "Purchased"
end
end

script.Parent.Parent.isItemPurchased.Changed:connect(isPurchased)

script.Parent.MouseButton1Click:connect(function()
	if script.Parent.Parent.isItemPurchased.Value == false then
		onClick()
	end
end)

1 answer

instead of doing this

script.Parent.MouseButton1Click:connect(function()
    if script.Parent.Parent.isItemPurchased.Value == false then
        onClick()
    end
end)

do this

script.Parent.MouseButton1Click:connect(function()
    if script.Parent.Parent.isItemPurchased.Value == false then
 player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
    cash = player.stats.Cash.Value
    tycoon = game.Workspace:FindFirstChild("tycoon_".. player.Name)
    print("type 1")
    if itemType == 1 then
        if cash >= itemCost then
            itemGrab = tycoon.tycoonPurchases:FindFirstChild(itemLocation)
            itemGrab.isPurchased.Value = true
            script.Parent.Parent.isItemPurchased.Value = true
        else
            script.Parent.Text = "Not Enough Funds"
            wait(3)
            script.Parent.Text = 'Purchase'
    end
    elseif itemtype == 2 then
    elseif itemtype == 3 then
    end
end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.