I want to make a system where a swarm of moths respawn after getting destroyed by the bug spray. The bug spray alone works perfectly fine but it is the respawn script that is causing me troubles. I wonder if there is some sort of connection or something I have to make between the two of them in order for it to work?
I have tried various methods but neither of them worked
ServerScript in Moths (Workspace model)
local originalPart = script.Parent.Swarm
local respawnTime = 1
local function respawnPart()
local newPart = originalPart:Clone()
newPart.Position = Vector3.new(-1.282, 5.812, 172.03)
newPart.Parent = game.Workspace
wait(respawnTime)
newPart:Destroy()
end
originalPart.AncestryChanged:Connect(function(_, parent)
if not parent then
respawnPart()
end
end)
ServerScript in Bug Spray (tool)
local Foam = script.Parent
local function onTouch(otherPart)
if otherPart.name == "DeathMoth" then
local DeathMoth = otherPart:FindFirstChild("DeathMoth") or otherPart:WaitForChild("DeathMoth")
local Smoke = otherPart:FindFirstChild("Smoke") or otherPart:WaitForChild("Smoke")
wait(0.5)
DeathMoth.Enabled = false
wait(1)
Smoke.Enabled = false
otherPart:destroy()
end
end
Foam.Touched:Connect(onTouch)