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

I cant make script understand if the tool is nil without errors?

Hello. I was making a script, that will give out a tool, and it need to check if the tool is nil. But, as it goes to the checking - it gives out that it is nil. I think, yeah, I know its nil, but, he supposed to give the tool after checking if the tool is nil. Thanks to anyone that will help. Here's the script -

script.Parent.MouseClick:Connect(function(player)
	print("Player is taking tool")
	if player.Backpack.Chips == nil then
		--i need something so he can understand if the chips is not in inventory 
		local Chips = game.ReplicatedStorage.Foods.Chips
		local Chipsclone = Chips:Clone()
		Chipsclone.Parent = player.Backpack
	end
end)

1 answer

✓ Accepted answer

I think this should work:

script.Parent.MouseClick:Connect(function(player)
    print("Player is taking tool")
    if not player.Backpack:FindFirstChild("Chips") then 
-- to see if an object exist we use if and we type the objects parent that we want to check if it is in or it exist under that folder and we use a function called :FindFirstChild(). This function will check if it exist, inside the () we put the name of the object like this ... ("Name") if it exist the script inside the if will start
        local Chips = game.ReplicatedStorage.Foods.Chips
        local Chipsclone = Chips:Clone()
        Chipsclone.Parent = player.Backpack
    end
end)

1 comment

0

Thank you! Works very well!

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