So I’m trying to make a mining system, I made it where if any rock is mined it will clone a few parts called “Stone” and a random Ore. But For some odd reason when I mine one rock it works but once i mine another rock I get this error and the stone parts won’t clone nor will the random ore. I didn’t know how to fix this, and tried to look up solutions but i couldn’t find much so far.
Here’s the script, sorry if its a bit long:
local stones = {}
local items = {}
local respawnTime = script.Parent.RespawnTime
--Services
local ss = game:GetService("ServerStorage")
local UIS = game:GetService("UserInputService")
--LootTable
local Drops = {
--Common
{Name = "Coal"},
{Name = "Copper"},
{Name = "Iron"},
{Name = "Lead"},
{Name = "Tin"},
--Uncommon
{Name = "Amber"},
{Name = "Gold"},
{Name = "Platinum"},
{Name = "Sapphire"},
{Name = "Topaz"},
{Name = "Tungesten"},
--Rare
{Name = "Cobalt"},
{Name = "Diamond"},
{Name = "Emerald"},
{Name = "Garnet"},
{Name = "Onyx"},
{Name = "Ruby"},
{Name = "SkyBlueTopaz"}
}
local lootTable = {
--Common Drops
{Item = Drops[1], Weight = 100}, --Coal
{Item = Drops[2], Weight = 100}, --Copper
{Item = Drops[3], Weight = 100}, --Iron
{Item = Drops[4], Weight = 100}, --Lead
{Item = Drops[5], Weight = 100}, --Tin
--Uncommon Drops
{Item = Drops[6], Weight = 45}, --Amber
{Item = Drops[7], Weight = 45}, --Gold
{Item = Drops[8], Weight = 45}, --Platinum
{Item = Drops[9], Weight = 45}, --Sapphire
{Item = Drops[10], Weight = 45}, --Topaz
{Item = Drops[11], Weight = 45}, --Tungesten
--Rare Drops
{Item = Drops[12], Weight = 15}, --Cobalt
{Item = Drops[13], Weight = 15}, --Diamond
{Item = Drops[14], Weight = 15}, --Emerald
{Item = Drops[15], Weight = 15}, --Garnet
{Item = Drops[16], Weight = 15}, --Onyx
{Item = Drops[17], Weight = 15}, --Ruby
{Item = Drops[18], Weight = 15}, --Sky Blue Topaz
}
local function returnSumOfWeight(lootTable)
local sum = 0
for _, entry in pairs(lootTable) do
sum = sum + entry.Weight
end
return sum
end
local function getRandomItem(lootTable)
local randomNumber = math.random(returnSumOfWeight(lootTable))
for _, entry in ipairs(lootTable) do
if randomNumber <= entry.Weight then
return entry.Item
else
randomNumber = randomNumber - entry.Weight
end
end
end
for i,object in pairs((script.Parent:GetChildren())) do --Will Get all of the stone
if(object:IsA("BasePart")) then
if(object:FindFirstChild("Health"))then
--Rock Variables
local label = object.HealthInfo
local hit_sound = script.Parent.Hit
local rockPos = object.Position
local Health = object.Health
local MaxHealth = object.MaxHealth
local RockMined = object.Mined.Value
--Mining Manager
object.Touched:Connect(function(otherPart)
local rockhit = false
local tool = otherPart.Parent
if tool:IsA('Tool') and tool.Mining.Value == true then
if rockhit == false then
rockhit = true
local damage = tool.Damage.Value
hit_sound:Play()
Health.Value = Health.Value - damage
label.TextLabel.Text = Health.Value.."/"..MaxHealth.Value
if(object.Health.Value <= 0) then --
if RockMined == false then
RockMined = true
local stone = ss.Drops.Normal:WaitForChild("Stone")
--Stone Generator
for i = 1, math.random(1,4) do
local clonedStone = stone:Clone()
table.insert(stones, #stones + 1, clonedStone)
end
for index, clonedStone in ipairs(stones) do
clonedStone.Position = rockPos
clonedStone.Parent = workspace
end
--Crystal/Ore Generator
local RandomDrop = math.random(96,99)
local item = getRandomItem(lootTable)
local Item = ss.Drops.Ores:FindFirstChild(item.Name)
for i = 1, math.random(1,3) do
local clonedItem = Item:Clone()
table.insert(items, #items + 1, clonedItem)
end
for index, clonedItem in ipairs(items) do
clonedItem.Position = rockPos
clonedItem.Parent = workspace
end
object:Destroy()
end
end
wait(0.05)
rockhit = false
end
end
end)
end
end
end