I’m trying to create an ore mining system. and I’d like to find a way to repeat the ore randomization so that an ore gets replaced after another ore gets destroyed. I’ve tried messing around with DescendantRemoving, but I wasn’t able to get much working. Any help would be most appreciated!
-- Pretending that Model is the model and OreScript is the script
Model.Destroying:Connect(function()
OreScript.Enabled = false
task.wait()
OreScript.Enabled = true
You can use CollectionService | Documentation - Roblox Creator Hub for that.
Example in a ServerScript:
local CollectionService = game:GetService("CollectionService") -- Make sure ores have a tag
local OreTag = "Ore" -- Or whatever tag name you used for ores
local function AddOreConnections(Ore)
Ore.Destroying:Once(function() -- Might have to change :Once to connect
local OreCFrame = Ore:GetPivot() -- Assuming model has a primary part
local RandomOre = ReplicatedStorage.Ores[math.random(1, #ReplicatedStorage.Ores:GetChildren())]:Clone() -- Get a new ore
RandomOre:PivotTo(OreCFrame) -- Can be placed anywhere, again assuming it has a primary part
RandomOre.Parent = Workspace -- Or where ever ores go
end
end
for _, Ore in CollectionService:GetTagged(OreTag) do -- Initalize ores that are already spawned in
AddOreConnections(Ore)
end
CollectionService.GetInstanceAddedSignal(OreTag):Connect(function(Ore)
AddOreConnections(Ore)
end)