Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your 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)
0
Thank you! Works very well! mariofl2003 11 — 4y
Ad

Answer this question