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

How to check if something exists?

So I'm trying to make a simple tycoon as practice for scripting. What I'm basically trying to do is make a tycoon button that appears only when another tycoon button is pressed and disappears. When I test this script out, both buttons appear and work fine, but I only want one of them at first, and then the other after I press on that one. The line I added to try and check to see if something exists is line 1. Thank you for the help and sorry for the mess of the script

if script.Parent.Parent.Parent.Products:findFirstChild("Conveyor") then

repeat wait() until script.Parent.Product.Value~=nil
local product=script.Parent.Product.Value:clone()
script.Parent.Product.Value:remove()
local price=script.Parent.Price.Value

script.Parent.Name=script.Parent.ProductName.Value.." - "..price
if price==0 then
	script.Parent.Name=script.Parent.ProductName.Value.." - Free!"
end

script.Parent.Head.Touched:connect(function(hit)
	chr=hit.Parent
	if chr:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(chr) then
		plr=game.Players:GetPlayerFromCharacter(chr)
		if plr.Name==script.Parent.Parent.Parent.OwnerName.Value and plr.leaderstats.Money.Value>=price then
			plr.leaderstats.Money.Value=plr.leaderstats.Money.Value-price
			product.Parent=script.Parent.Parent.Parent
			script.Parent:remove()
		end
	end
end)
end

-------------------------
--Background info
--script.Parent is the button
--script.Parent.Parent is a model of all the buttons
--script.Parent.Parent.Parent is the whole tycoon model
--script.Parent.Parent.Parent.Products is a model of all the products that the buttons buy
-------------------------

1 answer

✓ Accepted answer

What about using one script that handles all the buttons. Create all your buttons and have it clone and destroy the buttons you don't want to show yet.

When a button is pressed and the player has enough money the clone you created of the next button would be re-added to the model/workspace.

assuming that all your buttons are in a model and the script is in the model

local B = script.Parent.ButtonB:clone() 
	script.Parent.ButtonB:Destroy()

For example

when ButtonB is touched (This is the function)
	if the player has enough money ( your if statement)
		your code (whatever you want you code to do when the player has enough money)
		set clone B's parent to model (Now re-add button b to model)
		destroy ButtonA (remove button a from model)
	(else statement for when player doesn't have enough money)

Hope this helps.

3 comments

0

This sounds like it would work, but my tycoon isn't really normal and the buttons have different currencies to buy them with and sometimes they'll appear only if you complete a certain task, so it'd be much easier to insert a script into each of my buttons

0

Different currencies shouldn't be a problem. Just change your if statement to check the different currency and remove the different currency.

0

As for having buttons that are unlocked when you complete a certain task you could create a seperate model, called something like, TaskButtons, and then have the code handle all of that.

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