Problem locating a Folder in Workspace:FindFirstChild(player.Name)

Well, basically I have a problem when trying to locate a Folder that is in the player’s character and it gives me the following error: ServerScriptService.SystemSellMaterialAndDrop:11: attempt to index nil with 'WaitForChild' I really don’t know what the solution is

local Storage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Event = Storage:FindFirstChild("DatosDeMaterialDrop"):FindFirstChild("UnequipMaterialEvent")

local SellMaterial = workspace:WaitForChild("MaterialSystem"):WaitForChild("Sell")

SellMaterial.Touched:Connect(function(player)
	local character = game.Workspace:FindFirstChild(player.Name)
	local Player = Players:FindFirstChild(player.Name)

	local FolderMaterial = character:WaitForChild("Sistemas")

	if FolderMaterial:IsA("Part").Name == 'Tronco' then
		FolderMaterial.SistemaDeTala.Taken.Value = false
		player:WaitForChild("FolderValues").Dinero.Value = player.FolderValues.Dinero.Value + 7
		FolderMaterial:IsA("Part"):Destroy()
	elseif FolderMaterial:IsA("Part").Name == 'Rama' then
		FolderMaterial.SistemaDeTala.Taken.Value = false
		player:WaitForChild("FolderValues").Dinero.Value = player.FolderValues.Dinero.Value + 3
		FolderMaterial:IsA("Part"):Destroy()
	end
end)

There seems to be a considerable amount of flaws in your script. Whenever you’re using :FindFirstChild or WaitForChild, you want to make sure that the object you’re looking for actually exists before doing anything with it.

local character = game.Workspace:FindFirstChild(player.Name) --what would happen if character can't be found?

local FolderMaterial = character:WaitForChild("Sistemas") --if character is nil, you're trying to run a function from a nil value which is impossible.

This is an example of how it’s supposed to look like:

local character = game.Workspace:FindFirstChild(player.Name)
if character then --if character exists, then run. otherwise, skip.
	local FolderMaterial = character:WaitForChild("Sistemas")
	--other stuff
end

Consider reading this thread:

And also, I have no idea what you’re doing with the rest of your script. :IsA returns a boolean, not an instance:

FolderMaterial:IsA("Part"):Destroy()

As the other person said there are a few flaws in your script, but your current issue is that, when a part is touched it doesn’t return the player that touched it, it returns the basepart that touched it so to find the player you would do:

SellMaterial.Touched:Connect(function(part)
    if part.Parent:IsA("Model") and part.Parent.Humanoid then -- you can add more verifications if needed
        local character = part.Parent
        local player = Players:playerFromCharacter(character)
    end
end

and a little advice as a billingual person, you should code in one language because having multiple things in different languages can be confusing some times

1 Like

.Touched event returns a part, not a player instance.

Ignore what I previously said. Try defining the character by doing

 local character = player.Character