-
What do you want to achieve? Keep it simple and clear!
So i’m spawning models from a local code(cuz it requires to be like that) and i need to do movement code for each of this object, i tryed doing it with coroutine and function inside of spawn script, but it runs only once and stops:
SpawnCode:
function ClientFunctions:PhotonModelsHandler(player)
local function Movement(model)
local function newTrajectory()
local random = Random.new()
local randomTrajX = random:NextNumber(100, -100)
local randomTrajZ = random:NextNumber(100, -100)
local randomTrajY = random:NextNumber(100, 20)
model.AlignPosition.Position = Vector3.new(randomTrajX, randomTrajY, randomTrajZ)
end
if (model.Position - model.AlignPosition.Position).Magnitude < 1 then
print("fired")
newTrajectory()
Movement(model)
end
end
local function Increase(player)
local PlayerAmount = ValueFixer(ClientData.GetValue(player, "Photons", "Current"))
local PhotonsFolder = game:GetService("Workspace").FirstMap.Photons
local CurrentPhotons = PhotonModels["AMOUNT"]
local PhotonModel = game:GetService("ReplicatedStorage").Models.Photon
local ShinyPhotonModel = game:GetService("ReplicatedStorage").Models.ShinyPhoton
local ShinyChance = 1
if PlayerAmount >= InfiniteMath.new(100) then
local NewPhotons = 100 - CurrentPhotons
for i=1, NewPhotons do
if not PhotonsFolder:FindFirstChild("ShinyPhoton") then
if PhotonModels["GUARANTEE"] == 100 then
local ShinyClone = ShinyPhotonModel:Clone()
ShinyClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(ShinyClone)
end)
coroutine.resume(MovementThread)
else
local TryToSpawn = math.random(1, 100)
if TryToSpawn <= ShinyChance then
local ShinyClone = ShinyPhotonModel:Clone()
ShinyClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(ShinyClone)
end)
coroutine.resume(MovementThread)
else
local PhotonClone = PhotonModel:Clone()
PhotonClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(PhotonClone)
end)
coroutine.resume(MovementThread)
PhotonModels["GUARANTEE"] += 1
end
end
else
local PhotonClone = PhotonModel:Clone()
PhotonClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(PhotonClone)
end)
coroutine.resume(MovementThread)
end
CurrentPhotons = #PhotonsFolder:GetChildren()
PhotonModels["AMOUNT"] = CurrentPhotons
end
else
local NewPhotons = PlayerAmount:Reverse() - CurrentPhotons
for i=1, NewPhotons do
if not PhotonsFolder:FindFirstChild("ShinyPhoton") then
if PhotonModels["GUARANTEE"] == 100 then
local ShinyClone = ShinyPhotonModel:Clone()
ShinyClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(ShinyClone)
end)
coroutine.resume(MovementThread)
else
local TryToSpawn = math.random(1, 100)
if TryToSpawn <= ShinyChance then
local ShinyClone = ShinyPhotonModel:Clone()
ShinyClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(ShinyClone)
end)
coroutine.resume(MovementThread)
else
local PhotonClone = PhotonModel:Clone()
PhotonClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(PhotonClone)
end)
coroutine.resume(MovementThread)
PhotonModels["GUARANTEE"] += 1
end
end
else
local PhotonClone = PhotonModel:Clone()
PhotonClone.Parent = game:GetService("Workspace").FirstMap.Photons
local MovementThread = coroutine.create(function()
Movement(PhotonClone)
end)
coroutine.resume(MovementThread)
end
CurrentPhotons = #PhotonsFolder:GetChildren()
PhotonModels["AMOUNT"] = CurrentPhotons
end
end
end
local function Decrease(player)
local PlayerAmount = ValueFixer(ClientData.GetValue(player, "Photons", "Current"))
local PhotonsFolder = game:GetService("Workspace").FirstMap.Photons
local CurrentPhotons = #PhotonsFolder:GetChildren()
local ToDecrease = CurrentPhotons - PlayerAmount
for i=1, ToDecrease do
local Table = PhotonsFolder:GetChildren()
Table[i]:Destroy()
end
end
local PlayerAmount = ValueFixer(ClientData.GetValue(player, "Photons", "Current"))
local PhotonsFolder = game:GetService("Workspace").FirstMap.Photons
local CurrentPhotons = PhotonModels["AMOUNT"]
if PlayerAmount:Reverse() > CurrentPhotons then
Increase(player)
elseif PlayerAmount:Reverse() < CurrentPhotons then
Decrease(player)
end
end
Please note i can’t clone a local script into each model because it is spawned in workspace, where local scripts don’t run