For my game, I needed a script for making enemies spawn after entering an area, but before spawning the enemies, the previous enemies in the map needed to be removed to reduce lag.
Therefore, I have made a OnTouched script for detecting if there is children inside the global enemy folder, getting and destroying said children if they exist, and then cloning the folder containing the enemies to parent them to the global enemy folder.
I, however, had an issue that rendered the entire script non-functional. The script wasnât working so I checked the output and noticed that everytime I touched the part that was parent to the script, I would get a line on the output saying "Workspace.Spawn7B.Script:9: attempt to call missing method âDestroyâ of table ".
I have tried implementing a simple debounce but it broke the script entirely; not only did I not get the error, but nothing would even happen upon touching the parent part. Other than that, I donât really know what to do. My searches for similar issues have availed me nothing.
Here is the script:
local enemies = workspace.Enemies
function onTouched(part)
if part.Parent:findFirstChild("Humanoid")~=nil then -- check if humanoid exists (works)
if game.Workspace.Enemies:GetChildren()~=nil then -- check if there are instances inside the enemies folder (possible cause)
local oldenemies = workspace.Enemies:GetChildren()
oldenemies:Destroy()
end
local enemies = game.ServerStorage.Enemies.F2:Clone()
enemies.Parent = workspace.Enemies
wait(1)
end
end
script.Parent.Touched:connect(onTouched)
I have the feeling that the root of the problem is GetChildren(), but I do not know how else I may get and destroy the instances inside the enemy folder.