Collection Service Problem

Hey guys, I’m trying to make an ore mining system and I didn’t want each ore to have a script in it so I made a script using collection service and I’m pretty new to it so I don’t know what happened. The error I get is "ServerScriptService.MainOre:12: attempt to index nil with ‘Parent’ " but I’m not sure what to put instead of “Parent”. The tag is correct but I just don’t know what to do with the error. If I could get some help that would be great!

local CollectionService = game:GetService("CollectionService")

for i, ore in pairs(CollectionService:GetTagged("Ore")) do
	local health = 50
	local bool = true
	local ongoing = true
	local respawn = math.random(8,20)
	local oreName = ore.Name
	local ores = game.ReplicatedStorage.Ores:FindFirstChild(oreName)
	
	ore.Touched:Connect(function(ore, hit)
		if hit.Parent:FindFirstChild('IsAPick') and ongoing == true then -- error
			local status = hit.Parent:FindFirstChild('Type') -- error
			if health > 0 then
				if status.Value == 'Common' then
					health = health - 5
				elseif status.Value == 'Uncommon' then
					health = health - 10
				elseif status.Value == 'Rare' then
					health = health - 15
				elseif status.Value == 'Epic' then
					health = health - 20

				end
			elseif health <= 0 and bool == true then
				bool = false

				ore.Transparency = 1
				ore.CanCollide = false

				local wood = ores:Clone()
				wood.Parent = workspace
				wood:MoveTo(ore.Position)
				ongoing = false
				health = 50 -- [ Put the original health here ] --
				wait(respawn)
				ore.Transparency = 0
				ore.CanCollide = true
				bool = true
				ongoing = true
			end
		end
	end)
end

Hi, this may be happening because the hit parameter passed to the touched event handler doesn’t have a substantial Parent property. To circumvent this issue you can check in the event that the hit parameter isn’t nil to get to its Parent property.

iirc .Touched only has 1 parameter so use the ore value instead

1 Like

So are you saying to remove the function(ore, hit) and replace hit in the script with ore? Sorry if I’m being kind of dumb I just don’t know any better.

Here try this modified script. Sorry if it doesn’t work

ore.Touched:Connect(function(hit)
    if hit.Parent and hit.Parent:FindFirstChild('IsAPick') and ongoing == true then
        local status = hit.Parent:FindFirstChild('Type')
        if status then
            if health > 0 then
                if status.Value == 'Common' then
                    health = health - 5
                elseif status.Value == 'Uncommon' then
                    health = health - 10
                elseif status.Value == 'Rare' then
                    health = health - 15
                elseif status.Value == 'Epic' then
                    health = health - 20
                end
            elseif health <= 0 and bool == true then
                bool = false
                ore.Transparency = 1
                ore.CanCollide = false

                local wood = ores:Clone()
                wood.Parent = workspace
                wood:MoveTo(ore.Position)
                ongoing = false
                health = 50 -- Reset the health here --
                wait(respawn)
                ore.Transparency = 0
                ore.CanCollide = true
                bool = true
                ongoing = true
            end
        end
    end
end)
2 Likes

Hey thanks for helping me! This one worked!

Your welcome! Have a good day!!!

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