Instance checking issue

Hello everyone, I have been trying to make a bit of a tycoon-ish type of game and have been messing around adding some important code to my buttons for awhile now to get the ball rolling and I have been stress testing on whether or not I can get a certain script to warn me whenever I have invalid/conflicting configuration settings (in this case, I’m trying to get the script to warn me that there is no existent tycoon button with the name number identity of 0 that can be cloned into the workspace) to prevent troubling script breaks. Problem is, is I still get errors despite putting in the instance check here:

(in case you need an instance reference of whats in the workspace during the time this code would be used, I gave an example in the explorer.)

(Full Code Overview)

local debounce = 0
local price = script.Parent.Parent.Configuration.Price.Value

local config = script.Parent.Parent.Configuration
local buytargetname = script.Parent.Parent.Configuration.ObjectName.Value
local checkpoint = script.Parent.Parent.Configuration.Checkpoint.Value

local cmin = config.ButtonNumMin.Value
local cmax = config.ButtonNumMax.Value

local text1 = script.Parent.Parent.TextGui.Text1Sur.Text
local text2 = script.Parent.Parent.TextGui.Text2Sur.Text

text1.Text = ("Buy "..buytargetname.." for "..price)
text2.Text = ("Buy "..buytargetname.." for "..price)
function onTouched(hit)
	if debounce == 0
	then
		debounce = 1
		local check = hit.Parent:FindFirstChild("Humanoid")

		if check ~= nil then
				local user = game.Players:GetPlayerFromCharacter(hit.Parent)
				local stats = user:findFirstChild("leaderstats")
				if config.Multiplier.Value == true then
					if stats ~= nil then
						local cash = stats:findFirstChild("Currency")
						if cash.Value > price then
							cash.Value = cash.Value - price
							
						game:GetService("ServerScriptService").ServerConfig.CurrencyMultiplier.Value = game:GetService("ServerScriptService").ServerConfig.CurrencyMultiplier.Value*2
						print("Server Multiplier set to "..game:GetService("ServerScriptService").ServerConfig.CurrencyMultiplier.Value)
						if checkpoint == true then
							for i = cmin,cmax do
								if game:GetService("ReplicatedStorage").TycoonButtons["TycoonButton"..i] ~= nil then
									local buttonclone = game:GetService("ReplicatedStorage").TycoonButtons["TycoonButton"..i]:clone()
									buttonclone.Parent = workspace
								else
									warn("TycoonButton"..i" does not exist! Checkpoint invalid!")
							end
						end
						script.Parent.Parent:Destroy()
					end
						end
					end
					
					
				else
				if stats ~= nil then
					local cash = stats:findFirstChild("Currency")
					if cash.Value > price then
						cash.Value = cash.Value - price
						local clonedobj = game:GetService("ReplicatedStorage").TycoonObjects[buytargetname]:Clone()
						clonedobj.Parent = workspace
						if checkpoint == true then
					
							for i = cmin,cmax do 
								if game:GetService("ReplicatedStorage").TycoonButtons["TycoonButton"..i] ~= nil then
									local buttonclone = game:GetService("ReplicatedStorage").TycoonButtons["TycoonButton"..i]:clone()
									buttonclone.Parent = workspace
										else
							warn("TycoonButton"..i" does not exist! Checkpoint button spawning invalid!")
								end
								end
							
						end
							script.Parent.Parent:Destroy()
					end
				end
				end
			end
		end
	
		debounce = 0
		end
script.Parent.Touched:connect(onTouched)

Don’t really know how to fix this and I feel like I’m missing something super obvious here like a stray end being in the wrong line somehow (I checked even that and couldn’t find anything) but regardless I could use some help here. If you know anything, please do tell. I would appreciate it! :relaxed:

Looking at your hierarchy I don’t see a “TycoonButton0”. The script is erroring because that instance does not exist. I believe your ButtonNumMin value is too low, set it to 1.

You’re right. TycoonButton0 does not exist; however I’m trying to make the script do a check to see if it did exist in the case if I ever had the checkpoint boolvalue set to true on accident (the properties tab in the screenshot is an example of this). Problem that I have is that it’s not stating that it doesn’t exist and going through the else warn statement but rather breaking the script instead (which prevents the button from being destroyed).

Instead of

You’ve to use :FindFirstChild(“”) in your if statement
workspace.Object can’t be nil and so it will error
workspace:FindFirstChild(“Object”) can be nil and so it won’t error.

so it’d be:

if not game:GetService("ReplicatedStorage").TycoonButtons:FindFirstChild("TycoonButton"..i) then

and don’t you mean when game.RepStorage.TycoonButtons["TycoonButton"..i] is nil it will warn
and not when it’s not nil, as ~= means is not equal to.

Just tried this to make double sure it worked about half an hour ago. Don’t know why I suddenly forgot about the findfirstchild function. Anyway thank you!