How i can pass this infinite code to each cloned model?

  1. 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:
    image

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

Bump of topic, still can’t solve it

What do you mean by that it only moves once? Only one of the objects moves?

No every single object gets trajectory once, he should continue to get trajectory after he reached magnitude.

EDIT: Oops realised there is run of Movement in if loop

EDIT2: Not seems to work either, they just finish trajectory once and stop.

Bump, not solved still ( 30 letterssssssssss)

Hello 8 days letter and i still don’t really have any idae how to make movement code works infinite for those objects.

At no point have you put the Movement() thing inside of a loop, hence why its only running once. within the coroutines, add

while true do
    task.wait(1) -- Delay between trajectories
    Movement()
end

or something along those lines

Well won’t it stop this module script from running? Cuz this is main module of everything related to client, so it never should be delayed by any other scripts.

That’s why you do it in the coroutine! Coroutines allow code to run without interrupting the main code

Yeah probably. What about performance? Won’t while loop for each of existing objects cause performance issues?

Well it’s the only option that you have for it really.

Yeah i guess. Lemme try wrapping it into coroutine and check

Yeah it seems it for real best solution there. Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.