Attempting to index nil with 'Parent'

I made a quick Cup Giver script for a cafe game that I’m working on, but the cup giver isn’t working due to this bug. You can see the script below. The part that has puzzled me the most is that I’ve used this script previously in previous games, and even threw it into a new place and tested it out, and sure enough; it works. It’s only the game I’m currently working on where this error has occurred.

local GivePart = script.Parent.GivePart
local ClickDetector = GivePart.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cup = ReplicatedStorage.Cup

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
	local CupClone = Cup:Clone()
	CupClone.Parent = PlayerWhoClicked.Backpack
end)
1 Like

There probably isn’t a tool named Cup inside the ReplicatedStorage. Try adding a tool named Cup to the ReplicatedStorage and see if it works

1 Like

Try changing local Cup = ReplicatedStorage.Cup to
local Cup = ReplicatedStorage:WaitForChild("Cup") as the script might be creating the reference before the instance actually arrives in ReplicatedStorage.

image
Same error unfortunately.

1 Like

Check that Cup.Archivable = true. You can’t clone something if that’s false I think.

Ok, I did a quick recreation with an empty tool named “Cup” in ReplicatedStorage, using your code, and it worked. Is something deleting Cup from ReplicatedStorage after the server is started?

The same thing I experienced, I’ve used this script in multiple different games; it’s worked in every other game.

Did you check that the archivable property on your cup is true? Clone will return nil if that’s false. If the code was having issues finding the cup I believe it would error on the clone instead of when you set the parent.

The Cup tool has archivable set to true.

Try this

local GivePart = script.Parent.GivePart
local ClickDetector = GivePart.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cup = ReplicatedStorage.Cup

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
	local CupClone = Cup:Clone()
    print(Cup:GetFullName(), CupClone:GetFullName())
	CupClone.Parent = PlayerWhoClicked.Backpack
end)

And tell me what it says in the output.

image

Oops. I should have put those in separate print statements.
Do this instead

local GivePart = script.Parent.GivePart
local ClickDetector = GivePart.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cup = ReplicatedStorage.Cup

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
	local CupClone = Cup:Clone()
    print(Cup:GetFullName())
    print(Cup.Archivable) --Ensures another script hasn’t messed with this
    print(CupClone:GetFullName())
	CupClone.Parent = PlayerWhoClicked.Backpack
end)

I’m not sure if this will make any difference, but try using the code below. Let me know if you still get any errors

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cup = ReplicatedStorage:WaitForChild("Cup")

local GivePart = script.Parent:WaitForChild("GivePart")
local ClickDetector = GivePart.ClickDetector

ClickDetector.MouseClick:Connect(function(player)
	local Clone = Cup:Clone()
	Clone.Parent = player:WaitForChild("Backpack")
end)

Try changing the fourth line to local Cup = ReplicatedStorage.Cup:Clone()

This will only clone the Cup once, when the script runs. It should only be cloned whenever a player clicks on the ClickDetector.

All of these solutions just continue to reproduce the same error.
image

Could you show a screenshot of the Cup in ReplicatedStorage in the Explorer Menu?

image

Could you try these 2 code and show us the output? It could mean that there is another script interfering with the tool

local GivePart = script.Parent.GivePart
local ClickDetector = GivePart.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
    local Cup = ReplicatedStorage.Cup
    print(Cup)
    local CupClone = Cup:Clone()
    print(CupClone)
    CupClone.Parent = PlayerWhoClicked.Backpack
end)
local GivePart = script.Parent.GivePart
local ClickDetector = GivePart.ClickDetector
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Cup = ReplicatedStorage.Cup

ClickDetector.MouseClick:Connect(function(PlayerWhoClicked)
    print(Cup.Parent)
    local CupClone = Cup:Clone()
    print(CupClone)
    CupClone.Parent = PlayerWhoClicked.Backpack
end)

I know no other script is interfering with the tool; there are next to zero scripts currently in the game.
image

1 Like