Homing projectile goes off into distance

Hi, Im trying to make a homing missle rocket launcher which launches homing projectiles which tracks towards another player, however, when shot, they all go off into one direction in the distance no matter what

local Loader = require(game:GetService("ServerStorage"):WaitForChild("ToolPacks"):WaitForChild("WeaponsLoader"))
local tool = script.Parent
script.Parent.Equipped:Connect(function()
local part = game.ReplicatedStorage.Weapons.Rocket:Clone()

local stopDistance = 2.5 
	
	local player = Loader.Players:GetPlayerFromCharacter(tool.Parent)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local CF = player.Character.HumanoidRootPart.CFrame
	
	local velocityDirection = (part.Part.Position - character.HumanoidRootPart.Position).unit
	local Velocity = Vector3.new(velocityDirection.X, 0, velocityDirection.Z)
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Velocity = Velocity * 30
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Parent = part.Part
	part.Parent = workspace
	part.Part.CFrame = CF
local function Move(step)
	local function goal() 
			local players = game.Players:GetPlayers()
			
			local MaxDistance = 200
			local nearestTarget
			local user = player
			for i,v in pairs(players) do

				table.remove(players, 1, user)
				if (v.Character and v.Character.Name) then

					local target = v.Character
					local distance = (part.Part.Position - target.HumanoidRootPart.Position).Magnitude
					if distance > stopDistance then
						part.CFrame = part.CFrame:Lerp(target.HumanoidRootPart.CFrame, step * Velocity/distance)
						part.CFrame = CFrame.new(part.Part.Position, target.HumanoidRootPart.Position) * CFrame.Angles(math.rad(0), math.rad(90), math.rad(0))
					end
				end
			end
	end
	end

game:GetService("RunService").Stepped:Connect(function(time, step)
	Move(step)
end)
end)

The script looks very confusing, can you sort it out a bit?

sure, ill leave some comments on it


local Loader = require(game:GetService("ServerStorage"):WaitForChild("ToolPacks"):WaitForChild("WeaponsLoader"))
local tool = script.Parent


script.Parent.Equipped:Connect(function() -- detects when tool is equipped

local part = game.ReplicatedStorage.Weapons.Rocket:Clone() -- rocket clone in replicated storage

local stopDistance = 2.5 
	
	local player = Loader.Players:GetPlayerFromCharacter(tool.Parent)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local CF = player.Character.HumanoidRootPart.CFrame
	

local velocityDirection = (part.Part.Position - character.HumanoidRootPart.Position).unit -- adds velocity to rocket
	local Velocity = Vector3.new(velocityDirection.X, 0, velocityDirection.Z)
	
	local BodyVelocity = Instance.new("BodyVelocity")
	
	BodyVelocity.Velocity = Velocity * 30
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Parent = part.Part
	
	part.Parent = workspace
	part.Part.CFrame = CF


local function Move(step)
	local function goal()
			local players = game.Players:GetPlayers()
			
			local MaxDistance = 200
			local nearestTarget
			local user = player
			for i,v in pairs(players) do -- loops through all players to pick a target

				table.remove(players, 1, user) -- removes player shooting rocket launcher so rocket cant target them
				if (v.Character and v.Character.Name) then

					local target = v.Character
					local distance = (part.Part.Position - target.HumanoidRootPart.Position).Magnitude
					if distance <= MaxDistance then -- Checks if target is in range
						part.CFrame = part.CFrame:Lerp(target.HumanoidRootPart.CFrame, step * Velocity/distance) 
						part.CFrame = CFrame.new(part.Part.Position, target.HumanoidRootPart.Position) * CFrame.Angles(math.rad(0), math.rad(90), math.rad(0)) -- moves rocket towards target
					end
				end
			end
	end
	end

game:GetService("RunService").Stepped:Connect(function(time, step)
	Move(step) -- makes code run
end)
end)
1 Like

Why do you have a function in a function and not call it?

didnt notice that, i was origionally going to run code outside of it until i had to put it all in 1