So I have a Game Script which is practically the heart of my game, so far most of it has been working but I can't find an efficient way to remove all the enemies that spawn in while the match goes on.
This is how I spawn in enemies (including every variable used, the enemies spawn using wait() )
lightingenemies = game.Lighting.Enemies enemylist = lightingenemies:GetChildren() enemy = enemylist[math.random(1, #enemylist)] fieldenemy = enemy:clone() fieldenemy.Parent = game.Workspace fieldenemy:makeJoints()
I've tried these:
childd = game.Workspace:GetChildren() if childd.Name == enemylist.Name then childd:Remove()
childd = game.Workspace:GetChildren() if childd.Name == { "insert" , "thirty" , "different" , "names" , "here"} then --all the names of the units were put above child:Remove()
i've tried including models in the enemies themselves that makes it so they delete themselves when the value that tells the script if the game is running is false
if gamerun.Value == false then wait() script.Parent.Parent:Remove() -- the enemies were put inside another model for naming -- and so it wouldn't interfere with the enemy's code -- i've tried putting a while true do and that didn't work either
I'm positive there's a way to do this correctly. Any suggestions would be greatly appreciated, thank you!
Edit: I want to add on to this and apologize for the sloppy coding
Lots deprecated code you have. You have four parts of code, these parts will also be 4. They are in order and will correspond to your code.
Errors in your code:
Putting objects in Lighting
as storage is deprecated, use ReplicatedStorage
You misspelt Clone
as clone
and MakeJoints
as makeJoints
You tried to get the Name
of a table when that isn’t possible
You used Remove
when it’s Destroy
You tried to check if a table’s name equaled an entire table
Part1
local replicatedStorage = game:GetService"ReplicatedStorage" --Use this not Lighting local enemies = replicatedStorage.Enemies:GetChildren() -- i put enemies in a folder so when i do math.random i don’t get other non related objects local enemy = enemies[math.random(1, #enemies)] local fieldEnemy = enemy:Clone() -- Clone not clone! fieldEnemy.Parent = game.Workspace fieldEnemy:MakeJoints() --MakeJoints not makeJoints!
Part2
local children = game.Workspace:GetChildren() for i = 1, #children do --easier way to loop through tables if children[i].Name == replicatedStorage.Enemies.Name then -- tables don’t have names. objects do! children[i]:Destroy() --Destroy not remove! end end
Part3
local names = { ["insert"] = true, ["thirty"] = true, ["different"] = true, ["names"] = true, ["here"] = true } if names[children[i].Name] then --this is how to check if in table children[i]:Destroy() end
Part4
gamerun:GetPropertyChangedSignal('Value'):Connect(function() if gamerun.Value == false then wait() script.Parent.Parent:Destroy() end end