[Solved] If statement that checks if something doesn't exist. Doesn't work

Hi scripters,

i have a problem where my if statement that check if something doesn’t exist. Doesn’t work >:(
I am just making my own quest system for my game, so this is big problem.

The code:

local rmtEvent = myPathToIt

rmtEvent.Event:Connect(function(player, itemName)
	local questFolder = player.Quests
	local questItem = questFolder:FindFirstChild(itemName .. "Item")
	if  questItem == nil then
		print("This item is not in any quest!")
	elseif questItem  then
		questItem.Value += 1
		print(itemName .. ": " .. questItem.Value)
	end
	
end)

And i get this error: ServerScriptService.QuestChanged:5: attempt to index nil with 'FindFirstChild’

If something does not exists, I believe the output would be nil.

Example:

local item = script.Parent.Folder:FindFirstChild(“Item”)

if item == nil then
— Does not exist
else
— Exists

Hope this helps!

ServerScriptService.QuestChanged:5: attempt to index nil with 'FindFirstChild’ means that questFolder doesn’t exist, are you sure its there and you spelled it right?
Also you should change the “elseif not questItem then” to just else.

I think that the item i was looking for doesn’t exist, cus it will already make a error at creating the questFolder variable.

Ok i tried that but still the same error :confused:

Can you check if itemName exists and is not nil it might be caused by that. do print(itemName) and see what it says

Maybe try adding a WaitForChild function.

local questFolder = player:WaitForChild("Quests")

Ok the itemName is ok but still got the error.

Ill try it i hope that will work.

No still the same error but with WaitForChild :frowning:

Check the explorer when you’re playtesting. Do you see the Quest folder inside the player?

Yes i see it tbtvtcrcrfnnsnsnsns

How are you firing the event? What arguments do you pass over?

I use a bindable event, because i pass argument from script to script. And i pass the first argument as the player name( this could be the problem) and than the item name.

You’re correct. The player’s name is a string, not an instance.

1 Like

Yeah i just realised it XD Because before i wanted to print the players name so i putted in in the argument. :smiley:

1 Like

It’s working ty for your help :heart: :smiley:

1 Like