Why is this printing nil?

Hello!

So I am trying to create a Find The… game in which you have to find the memes, but my collection system is not working.

Collection script:

local meme = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MemeList = ReplicatedStorage:WaitForChild("Memes")
local Baller = MemeList:WaitForChild("Baller")

meme.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			local collectedFile = player.MemesCollected
			local found = collectedFile:FindFirstChild("Baller") -- I think this the problem line
			print(found) -- Prints "nil"
		end
	end
end)

Whenever the meme is touched, it should add it to a folder parented to the player.
I think what it’s doing is it’s searching for the Baller value and it thinks it should already be in the folder, even though it’s not. If this is the problem, I’m just not sure how to fix it.

Any help is appreciated! Thank you and have a wonderful day! :slight_smile:

Can you send a picture of the “Memes” folder in ReplicatedStorage?

image

This code should address your issues.

local meme = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MemeList = ReplicatedStorage:WaitForChild("Memes")
local Baller = MemeList:WaitForChild("Baller")

meme.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			local collectedFile = player.MemesCollected
			local found = collectedFile:FindFirstChild("Baller").Value -- I think this the problem line
			print(found) -- Prints "nil"
		end
	end
end)

You are correct, the findfirstchild is returning nil because “baller” isn’t there.
You will need to clone the “baller” that is currently in replicated storage and parent it to the “collectedFile”.

--Insert this after local collectedFile = player.MemesCollected
local clone = Baller:Clone()
clone.Parent = collectedFile
local found = collectedFile:FindFirstChild("Baller") --You won't actually need this line, you can just refer to the clone (just left it in to demonstrate)
print(found, found.Value) --Should now print Baller with any default value it had already been given whilst in rep storage
1 Like

It worked! Thank you so much!

Edit: I got this from a tutorial and was trying to tweak it to fit into my game and later in the video they cloned it as well lol.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.