Hello everyone ! I need help with my game because when i want to teleport npc to a random spawn, i’ve seen it teleport on the same one.
There is the script (serverscriptservice) :
-- Entitys --
local Statue1 = game.Workspace.GameAI.Angel
local Statue2 = game.Workspace.GameAI.Angel2
local Statue3 = game.Workspace.GameAI.Angel3
local Statue4 = game.Workspace.GameAI.Angel4
local Statue5 = game.Workspace.GameAI.Angel5
local Statue6 = game.Workspace.GameAI.Angel6
local CStatue1 = game.Workspace.GameAI.CorruptedAngel
local CStatue2 = game.Workspace.GameAI.CorruptedAngel2
local CStatue3 = game.Workspace.GameAI.CorruptedAngel3
-- SpawnParts --
local AngelPart1 = game.Workspace.AngelSpawn.Angel1
local AngelPart1 = game.Workspace.AngelSpawn.Angel2
local AngelPart1 = game.Workspace.AngelSpawn.Angel3
local AngelPart1 = game.Workspace.AngelSpawn.Angel4
local AngelPart1 = game.Workspace.AngelSpawn.Angel5
local AngelPart1 = game.Workspace.AngelSpawn.Angel6
local CAngelPart1 = game.Workspace.AngelSpawn.CorruptedAngel1
local CAngelPart2 = game.Workspace.AngelSpawn.CorruptedAngel2
local CAngelPart3 = game.Workspace.AngelSpawn.CorruptedAngel3
-- Events --
local Starting = game.ReplicatedStorage.StartingForServer
local Finished = game.ReplicatedStorage.FinishedForServer
-- Functions --
Starting.Event:Connect(function()
for i, entity in pairs(game.Workspace.GameAI:GetChildren()) do
if entity.Name == "Angel" or entity.Name == "Angel2" or entity.Name == "Angel3" or entity.Name == "Angel4" or entity.Name == "Angel5" or entity.Name == "Angel6" then
for i, Spawner in pairs(game.Workspace.AngelSpawn:GetChildren()) do
if Spawner.Name == "Angel1" or Spawner.Name == "Angel2" or Spawner.Name == "Angel3" or Spawner.Name == "Angel4" or Spawner.Name == "Angel5" or Spawner.Name == "Angel6" then
if Spawner.Value.Value == nil then
entity:WaitForChild("HumanoidRootPart").CFrame = Spawner.CFrame
Spawner.Value.Value = entity
entity:WaitForChild("HumanoidRootPart").Anchored = false
print(entity.Name.." spawn at "..Spawner.Name)
break
end
end
end
elseif entity.Name == "CorruptedAngel" or entity.Name == "CorruptedAngel2" or entity.Name == "CorruptedAngel3" then
for i, Spawner in pairs(game.Workspace.AngelSpawn:GetChildren()) do
if Spawner.Name == "CorruptedAngel1" or Spawner.Name == "CorruptedAngel2" or Spawner.Name == "CorruptedAngel3" then
if Spawner.Value.Value == nil then
entity:WaitForChild("HumanoidRootPart").CFrame = Spawner.CFrame
Spawner.Value.Value = entity
entity:WaitForChild("HumanoidRootPart").Anchored = false
print(entity.Name.." spawn at "..Spawner.Name)
if Spawner.Name == "CorruptedAngel1" then
entity.PathValue.Value = 1
end
if Spawner.Name == "CorruptedAngel2" then
entity.PathValue.Value = 2
end
if Spawner.Name == "CorruptedAngel3" then
entity.PathValue.Value = 3
end
break
end
end
end
end
end
end)
Finished.Event:Connect(function()
for i, Spawner in pairs(game.Workspace.AngelSpawn:GetDescendants()) do
Spawner.Value.Value = nil
end
for i, entity in pairs(game.Workspace.GameAI:GetChildren()) do
entity:WaitForChild("HumanoidRootPart").CFrame = game.Workspace.AngelsBaseSpawn.CFrame
entity:WaitForChild("HumanoidRootPart").Anchored = true
if entity.Name == "CorruptedAngel" or entity.Name == "CorruptedAngel2" or entity.Name == "CorruptedAngel3" then
entity.PathValue.Value = 0
end
end
end)
This is the print (It’s the same all time) :
There are a multiple of npc named angels and corrupted angels.
Help me please
-- SpawnParts --
local AngelPart1 = game.Workspace.AngelSpawn.Angel1
local AngelPart1 = game.Workspace.AngelSpawn.Angel2
local AngelPart1 = game.Workspace.AngelSpawn.Angel3
local AngelPart1 = game.Workspace.AngelSpawn.Angel4
local AngelPart1 = game.Workspace.AngelSpawn.Angel5
local AngelPart1 = game.Workspace.AngelSpawn.Angel6
i dont know if it will fix the code, but you forgot tho change the variable name, is overwriting it and deleting the past value
local corrupteds = {"c1","c2","c3","c4","c5"}
local corruptedsSpawn = {"cs1","cs2","cs3","cs4","cs5"}
--You showld get this tables from a ":GetChildrend()" of a folder like "workspace.Angels:GetChildren()" or "workspace.CorruptedAngels:GetChildren()"
local function printArray(t) for i, v in pairs(t) do warn(i,v) end end
local function sortSpawns (entities,spawns) -- table1 = angles, table2 = spawns
local spawnsToSpawn = {} -- a new table
for _, spwn in pairs(spawns) do --set the table
table.insert(spawnsToSpawn,spwn) -- the spawn is free to use bc is in the table!
end
local result = {} -- a dictonary to return
--printArray(spawnsToSpawn)
for _, ent in pairs(entities) do --for every thing to spawn
local randomSpanwIndex = math.random(1,#spawnsToSpawn) -- we search a random spawn in the table
result[ent] = spawnsToSpawn[randomSpanwIndex] --we set an angel to a that random spawn in the dictionary
table.remove(spawnsToSpawn,randomSpanwIndex) --we remove the spawn from the table of spawn bc it is not free anymore
--warn(#spawnsToSpawn) -- the amount of spawns lefts
end
--printArray(result)
return result
end
local res = sortSpawns(angels,angelsSpawns)
for ang, spawn in pairs(res) do
--[[
do your stuff like this:
ang.Position = spawn.Position
]]
end
local res = sortSpawns(corrupteds,corruptedsSpawn)
for corruptedAng, spawn in pairs(res) do
--[[
do your stuff like this:
corruptedAng.Position = spawn.Position
]]
end
local angels = {}
for i, angel in pairs(game.Workspace.GameAI:GetChildren()) do
if angel.Name == "Angel" or angel.Name == "Angel2" or angel.Name == "Angel3" or angel.Name == "Angel4" or angel.Name == "Angel5" or angel.Name == "Angel6" then
table.insert(angels, angel)
end
end
local angelsSpawns = {}
for i, angel in pairs(game.Workspace.AngelSpawn:GetChildren()) do
if angel.Name == "Angel" or angel.Name == "Angel2" or angel.Name == "Angel3" or angel.Name == "Angel4" or angel.Name == "Angel5" or angel.Name == "Angel6" then
table.insert(angels, angel)
end
end
local corrupteds = {}
for i, angel in pairs(game.Workspace.GameAI:GetChildren()) do
if angel.Name == "CorruptedAngel" or angel.Name == "CorruptedAngel2" or angel.Name == "CorruptedAngel3" then
table.insert(corrupteds, angel)
end
end
local corruptedsSpawns = {}
for i, angel in pairs(game.Workspace.AngelSpawn:GetChildren()) do
if angel.Name == "CorruptedAngel1" or angel.Name == "CorruptedAngel2" or angel.Name == "CorruptedAngel3" then
table.insert(corruptedsSpawns, angel)
end
end
print(angels)
print(angelsSpawns)
print(corrupteds)
print(corruptedsSpawns)
--You showld get this tables from a ":GetChildrend()" of a folder like "workspace.Angels:GetChildren()" or "workspace.CorruptedAngels:GetChildren()"
local function printArray(t) for i, v in pairs(t) do warn(i,v) end end
local function sortSpawns (entities,spawns) -- table1 = angles, table2 = spawns
local spawnsToSpawn = {} -- a new table
for _, spwn in pairs(spawns) do --set the table
table.insert(spawnsToSpawn,spwn) -- the spawn is free to use bc is in the table!
end
local result = {} -- a dictonary to return
printArray(spawnsToSpawn)
for _, ent in pairs(entities) do --for every thing to spawn
local randomSpanwIndex = math.random(1,#spawnsToSpawn) -- we search a random spawn in the table
result[ent] = spawnsToSpawn[randomSpanwIndex] --we set an angel to a that random spawn in the dictionary
table.remove(spawnsToSpawn,randomSpanwIndex) --we remove the spawn from the table of spawn bc it is not free anymore
--warn(#spawnsToSpawn) -- the amount of spawns lefts
end
printArray(result)
return result
end
local res = sortSpawns(angels,angelsSpawns)
for ang, spawn in pairs(res) do
--[[
do your stuff like this:
ang.Position = spawn.Position
]]
end
local res = sortSpawns(corrupteds,corruptedsSpawns)
for corruptedAng, spawn in pairs(res) do
--[[
do your stuff like this:
corruptedAng.Position = spawn.Position
]]
end