Help tweening multiple models to move towards player, only first model will currently move

  1. What do you want to achieve?
    Want to tween multiple models towards the player.

  2. What is the issue?
    With the script I have currently, each model will get cloned in to a folder under workspace, the first model’s script will run fine. Any models after will not move as if the script is not being run.

    Folder setup

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Have even tried using MoveTo(), but that did not work either. Also have tried using pairs within the MoveScript with no luck.

    SpawnScript located in ServerScriptService

    local TweenService = game:GetService("TweenService")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local ServerScriptService = game:GetService("ServerScriptService")
    
    function spawnModel(name)
    	local enemy = ReplicatedStorage.Models:WaitForChild("Model"):Clone()
    	enemy.Parent = workspace.Models
    	enemy.Name = enemy.Name..name
    
    	enemy:PivotTo(enemy:GetPivot() + Vector3.new(0, 0, 10))
    end
    
    local a = 0
    
    while a <= 3 do
    	spawnModel(a)	
    	a = a + 1
    	wait(1)
    end
    

    MoveScript located in the Model, which is located in ReplicatedStorage

    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local getPlayerPosition = Instance.new("RemoteEvent")
    local TweenService = game:GetService("TweenService")
    
    local modelBlock = script.Parent
    
    getPlayerPosition.Name = "GetPlayerPosition"
    getPlayerPosition.Parent = ReplicatedStorage
    
    local function onGetPlayerPosition()
    	--
    end
    
    function moveModel(model, player)	
    	local goal = player
    
    	local tweenInfo = TweenInfo.new(3)
    	
    	local tween = TweenService:Create(model.PrimaryPart, tweenInfo, {CFrame = CFrame.new(goal)})
    	
    	tween:Play()
    end
    
    while wait(1) do
    	local currentPosition = getPlayerPosition.OnServerEvent:Wait(onGetPlayerPosition).Character.PrimaryPart.Position
    	moveModel(modelBlock, currentPosition)
    end
    

    PlayerPositionScript located in StarterPlayerScripts

    -- Script to send player position to server
    local player = game.Players.LocalPlayer
    repeat wait() until player.Character
    local Humanoid = player.Character:WaitForChild("Humanoid")
    local Players = game:GetService("Players")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    
    local targetPlayer = Humanoid.RootPart.Position
    local getPlayerPosition = ReplicatedStorage:WaitForChild("GetPlayerPosition")
    
    -- Send player position
    while true do
    	targetPlayer = Humanoid.RootPart.Position
    	getPlayerPosition:FireServer(targetPlayer)
    	wait(1)
    end
    

Just in case anyone needs this in the future, I solved this by using for i, v in pairs within the moveModel function.

-- Function to move the model towards the player
function moveModel(model, speed)
	while wait(1) do
		-- Get all children within workspace
		for i, v in pairs(workspace:GetChildren()) do
			-- Find the first Humanoid child
			if v:findFirstChildOfClass("Humanoid") then
				-- Get player position, this will be the tween goal
				local goal = v.PrimaryPart.Position
				-- Tween setup info
				local tweenInfo = TweenInfo.new(speed)
				-- Setup the tween
				local tween = TweenService:Create(model.PrimaryPart, tweenInfo, {CFrame = CFrame.new(goal)})
				-- Run the tween
				tween:Play()
				
				-- Model touched				
				model.PrimaryPart.Touched:Connect(onTouched)
				
				-- Enemy Firing
				enemyFire(ENEMY_PROJECTILE_SPEED)
				
				-- If enemy goes past player, destroy enemy
				if script.Parent.PrimaryPart.Position.Z > v.PrimaryPart.Position.Z  then
					script.Parent:Destroy()
				end
				
				wait(0.5)
			end
		end
	end
end

-- Run the function
moveModel(script.Parent, MODEL_SPEED)

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