What do you want to achieve?
I would like the opponent to respawn on each platform and take into account the attributes connected to a given respawn.
What is the issue?
I would like to respawn my opponent on a respawn platform, but I need to extract the value from the table one by one, and all the time it only extracts the first index and ignores the second one, what should I do?
What solutions have you tried so far?
I have looked at every possible way…
– ( If anyone has any doubts about my purpose, you can ask and I will try to explain it thoroughly )
spawnerlist = {game.Workspace.Level1.Respawns:GetChildren()}
for i, v in pairs(spawnerlist) do
spawner = v[i]
while wait(1) do
print(i)
print(v)
end
end
local respawnTime = spawner:GetAttribute("RespawnTime")
local enemyName = spawner:GetAttribute("EnemyName")
local enemy = game.ServerStorage.Enemies[enemyName]
local spawnedEnemy = nil
function despawnEnemy()
if spawnedEnemy then
spawnedEnemy:Destroy()
spawnedEnemy = nil
end
end
function spawnEnemy()
despawnEnemy()
local clonedEnemy = enemy:Clone()
spawnedEnemy = clonedEnemy
clonedEnemy.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0,3,0)
clonedEnemy.Parent = workspace
end
while true do
spawnEnemy()
local humanoid : Humanoid = spawnedEnemy:WaitForChild("EnemyHumanoid")
humanoid.Died:Wait()
task.wait(respawnTime)
end
There are a few things wrong with this code. GetChildren() already returns a table. If you are not planning on having multiple tables, wrapping the method with {} will not work. (Refering to the line below)
Now, your for loop was just running the first index because you are using a while loop inside of it. Loops in lua will yield the code, meaning they pause the code until they finish during their task, and since you are using an infinite loop, it will never run the next index or any of the remaining code.
I was a little confused on the goal of the code but I tried fixing it. It’s early in the morning so forgive me if I missed some things.
local spawnerlist = game.Workspace.Level1.Respawns:GetChildren()
function despawnEnemy(spawnedEnemy)
if spawnedEnemy then
spawnedEnemy:Destroy()
spawnedEnemy = nil
end
end
function spawnEnemy(enemy,spawnPoint,spawnedEnemy)
despawnEnemy(spawnedEnemy)
local clonedEnemy = enemy:Clone()
local spawnedEnemy = clonedEnemy
clonedEnemy.HumanoidRootPart.CFrame = spawnPoint.CFrame + Vector3.new(0,3,0)
clonedEnemy.Parent = workspace
return spawnedEnemy
end
for _,spawnPoint in spawnerlist do
local respawnTime = spawnPoint:GetAttribute("RespawnTime")
local enemyName = spawnPoint:GetAttribute("EnemyName")
local enemy = game.ServerStorage.Enemies[enemyName]
local spawnedEnemy = nil
local humanoidConnection = nil
humanoidConnection = spawnedEnemy:WaitForChild("EnemyHumanoid").Died:Connect(function()
task.delay(respawnTime,function()
spawnEnemy(enemy,spawnPoint,spawnedEnemy)
end)
humanoidConnection:Disconnect()
end)
end
it’s because GetChildren() already returns a table and u put this GetChildren() table inside of another tabe so there is only one value inside of the spawner table. Remove the curly brackets and I think u will be fine.
To avoid confusion, I will explain more what the code is about
There are 2 platforms A and B (image 1) etc., each platform has its own attributes (image 2) and the code first checks which enemy will respawn and then how long it will take to respawn after being killed.
The main problem was that Humanoid would either appear on one of the platforms or not at all.
The corrected code now doesn’t work because humanoidConnection always returns nil
Yes. I just saw I forgot one important part of it. try this
local spawnerlist = game.Workspace.Level1.Respawns:GetChildren()
function despawnEnemy(spawnedEnemy)
if spawnedEnemy then
spawnedEnemy:Destroy()
spawnedEnemy = nil
end
end
function spawnEnemy(enemy,spawnPoint,spawnedEnemy)
despawnEnemy(spawnedEnemy)
local clonedEnemy = enemy:Clone()
clonedEnemy.HumanoidRootPart.CFrame = spawnPoint.CFrame + Vector3.new(0,3,0)
clonedEnemy.Parent = workspace
return clonedEnemy
end
for _,spawnPoint in spawnerlist do
local respawnTime = spawnPoint:GetAttribute("RespawnTime")
local enemyName = spawnPoint:GetAttribute("EnemyName")
local enemy = game.ServerStorage.Enemies[enemyName]
local spawnedEnemy = nil
local humanoidConnection = nil
spawnedEnemy = spawnEnemy(enemy,spawnPoint,spawnedEnemy)
humanoidConnection = spawnedEnemy:WaitForChild("EnemyHumanoid").Died:Connect(function()
task.delay(respawnTime,function()
spawnEnemy(enemy,spawnPoint,spawnedEnemy)
end)
humanoidConnection:Disconnect()
end)
end
local spawnerlist = game.Workspace.Level1.Respawns:GetChildren()
function despawnEnemy(spawnedEnemy)
if spawnedEnemy then
spawnedEnemy:Destroy()
end
end
function spawnEnemy(spawnPoint,spawnedEnemy,respawnTime,enemy)
despawnEnemy(spawnedEnemy)
spawnedEnemy = enemy:Clone()
spawnedEnemy.HumanoidRootPart.CFrame = spawnPoint.CFrame + Vector3.new(0,3,0)
spawnedEnemy.Parent = workspace
local humanoidConnection = nil
humanoidConnection = spawnedEnemy:WaitForChild("EnemyHumanoid").Died:Connect(function()
task.delay(respawnTime,function()
spawnEnemy(enemy,spawnPoint,spawnedEnemy,respawnTime,enemy)
end)
humanoidConnection:Disconnect()
end)
end
for _,spawnPoint in spawnerlist do
local respawnTime = spawnPoint:GetAttribute("RespawnTime")
local enemyName = spawnPoint:GetAttribute("EnemyName")
local enemy = game.ServerStorage.Enemies[enemyName]
spawnEnemy(spawnPoint,nil,respawnTime,enemy)
end
The code you posted doesn’t work and removes my spawn point, but I’ve already resolved all the issues
Snippet my code:
local spawnerlist = game.Workspace.Level1.Respawns:GetChildren()
function despawnEnemy(spawnedEnemy)
if spawnedEnemy then
spawnedEnemy:Destroy()
end
end
function spawnEnemy(enemyModel, spawnPoint)
local clonedEnemy = enemyModel:Clone()
clonedEnemy.HumanoidRootPart.CFrame = spawnPoint.CFrame + Vector3.new(0,3,0)
clonedEnemy.Parent = workspace
return clonedEnemy
end
for _, spawnPoint in ipairs(spawnerlist) do
local respawnTime = spawnPoint:GetAttribute("RespawnTime")
local enemyName = spawnPoint:GetAttribute("EnemyName")
local enemyModel = game.ServerStorage.Enemies[enemyName]
local function spawnNewEnemy()
local spawnedEnemy = spawnEnemy(enemyModel, spawnPoint)
local humanoidConnection
humanoidConnection = spawnedEnemy:WaitForChild("EnemyHumanoid").Died:Connect(function()
humanoidConnection:Disconnect()
task.delay(respawnTime, function()
despawnEnemy(spawnedEnemy)
spawnNewEnemy()
end)
end)
end
spawnNewEnemy()
end