CFrame not Setting on cloned part inside of For I = 1

i been having issues with my code, does work, but some how doesnt Cframe the Part to the “Start”,
and yes i check everything, down if is not nil the part im setting on, or if im using something wrong:

local module = {}

function module.SimpleProjectile(player, Char, Params: {})
	local Time = Params.TimeLeft
	local HitTimes = Params.Hits
	local Speed = Params.Speed
	local Model = Params.model
	local StartPoint = Params.Start
	local direction = Params.Direccion
	
	local character = player.Character
	local Projectile: Object = game:GetService("ServerStorage"):FindFirstChild("MainStorage"):FindFirstChild("ItemsToClone").Projectiles[Model or "DefaultOne"]:Clone()
	Projectile.Parent = workspace.Effects
	
	if Projectile:IsA("Model") or Projectile:IsA("Folder") then
		for i, v in pairs(Projectile:GetDescendants()) do
			if v:IsA("Part") or v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
				v.Anchored = false
				v.CanCollide = false
				v.Massless = true
			end
		end
	else
		Projectile.Anchored = false
		Projectile.CanCollide = false
		Projectile.Massless = true
	end
	
	local body = Instance.new("BodyVelocity", Projectile)
	body.MaxForce = Vector3.new(1e6,1e6,1e6)
	body.Velocity = CFrame.new()
	
	return Projectile
end

local function applyProjectilePhysics(projectile, direction, speed)
	local body = Instance.new("BodyVelocity")
	body.MaxForce = Vector3.new(1e6, 1e6, 1e6)
	body.Velocity = direction.Unit * speed
	body.P = 1250
	body.Parent = projectile
end

function module.ProjectileRandomSpread(player, char, Params: {})
	local Time = Params.TimeLeft or 2
	local HitTimes = Params.Hits or 1
	local Speed = Params.Speed or 100
	local Model = Params.model
	local StartPoint = Params.Start
	local BaseDirection = Params.Direccion
	local SpreadAngle = Params.SpreadAngle or 10 -- degrees
	local PelletCount = Params.Pellets or 5

	local MainStorage = game:GetService("ServerStorage"):FindFirstChild("MainStorage")
	local ProjectilesFolder = MainStorage and MainStorage:FindFirstChild("ItemsToClone") and MainStorage.ItemsToClone:FindFirstChild("Projectiles")
	local BaseProjectile = ProjectilesFolder and ProjectilesFolder[Model or "DefaultOne"]

	if not BaseProjectile then
		warn("Projectile model not found!")
		return
	end

	local projectiles = {}
	
	task.spawn(function()

		for i = 1, PelletCount do
			local direction = BaseDirection

			-- Apply spread if needed
			if PelletCount > 1 and SpreadAngle > 0 then
				local randomAngle = math.rad(SpreadAngle)
				local rand = Random.new()
				local x = rand:NextNumber(-randomAngle, randomAngle)
				local y = rand:NextNumber(-randomAngle, randomAngle)
				local z = rand:NextNumber(-randomAngle, randomAngle)
				local offset = CFrame.Angles(x, y, z)
				direction = (CFrame.new(Vector3.zero, BaseDirection) * offset).LookVector
			end
			local Projectile = BaseProjectile:Clone()

			if Projectile:IsA("Model") then
				for _, v in pairs(Projectile:GetDescendants()) do
					if v:IsA("BasePart") then
						v.Anchored = false
						v.CanCollide = false
						v.Massless = true
					end
				end
				if Projectile.PrimaryPart then
					Projectile:SetPrimaryPartCFrame(CFrame.new(StartPoint))
					applyProjectilePhysics(Projectile.PrimaryPart, direction, Speed)
				else
					warn("Projectile model has no PrimaryPart")
				end
				Projectile.Parent = workspace.Effects
			else
				Projectile.Parent = workspace.Effects
				Projectile.CFrame = CFrame.new(StartPoint)
				Projectile.Anchored = false
				Projectile.CanCollide = false
				Projectile.Massless = true
				applyProjectilePhysics(Projectile, direction, Speed)
			end

			-- Auto-destroy after time
			game.Debris:AddItem(Projectile, Time)

			table.insert(projectiles, Projectile)
		end
	end)


	return projectiles
end

return module

Which function is experiencing the problem?

If it’s module.SimpleProjectile, I think you just forgot to apply StartPoint.

no, i found out, is the “table.insert” that is making everything an issue, basicly is a bad timing running the code

but the problem, i need to insert the part in the “Projectiles” table most quickly as possible

How does that cause any issues?

Also why are you using task.spawn if your code doesn’t yield?

trust me i dont know why that causes issues, and uhh i thought spawn coulded fix it, i forgor about it

I really don’t think that’s your issue, can you show more of your code?

nvm i just fixed, by

			task.defer(function()
				Projectile:PivotTo(CFrame.new(StartPoint))
			end)
local module = {}

function module.SimpleProjectile(player, Char, Params: {})
	local Time = Params.TimeLeft
	local HitTimes = Params.Hits
	local Speed = Params.Speed
	local Model = Params.model
	local StartPoint = Params.Start
	local direction = Params.Direccion
	
	local character = player.Character
	local Projectile: Object = game:GetService("ServerStorage"):FindFirstChild("MainStorage"):FindFirstChild("ItemsToClone").Projectiles[Model or "DefaultOne"]:Clone()
	Projectile.Parent = workspace.Effects
	
	if Projectile:IsA("Model") or Projectile:IsA("Folder") then
		for i, v in pairs(Projectile:GetDescendants()) do
			if v:IsA("Part") or v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
				v.Anchored = false
				v.CanCollide = false
				v.Massless = true
			end
		end
	else
		Projectile.Anchored = false
		Projectile.CanCollide = false
		Projectile.Massless = true
	end
	
	local body = Instance.new("BodyVelocity", Projectile)
	body.MaxForce = Vector3.new(1e6,1e6,1e6)
	body.Velocity = CFrame.new()
	
	return Projectile
end

local function applyProjectilePhysics(projectile, direction, speed)
	local body = Instance.new("BodyVelocity")
	body.MaxForce = Vector3.new(1e6, 1e6, 1e6)
	body.Velocity = direction.Unit * speed
	body.P = 1250
	body.Parent = projectile
end

function module.ProjectileRandomSpread(player, char, Params: {})
	local Time = Params.TimeLeft or 2
	local HitTimes = Params.Hits or 1
	local Speed = Params.Speed or 100
	local Model = Params.model
	local StartPoint = Params.Start
	local BaseDirection = Params.Direccion
	local SpreadAngle = Params.SpreadAngle or 10 -- degrees
	local PelletCount = Params.Pellets or 5

	local MainStorage = game:GetService("ServerStorage"):FindFirstChild("MainStorage")
	local ProjectilesFolder = MainStorage and MainStorage:FindFirstChild("ItemsToClone") and MainStorage.ItemsToClone:FindFirstChild("Projectiles")
	local BaseProjectile = ProjectilesFolder and ProjectilesFolder[Model or "DefaultOne"]

	if not BaseProjectile then
		warn("Projectile model not found!")
		return
	end

	local projectiles = {}
	
	for i = 1, PelletCount do
		local direction = BaseDirection

		-- Apply spread if needed
		if PelletCount > 1 and SpreadAngle > 0 then
			local randomAngle = math.rad(SpreadAngle)
			local rand = Random.new()
			local x = rand:NextNumber(-randomAngle, randomAngle)
			local y = rand:NextNumber(-randomAngle, randomAngle)
			local z = rand:NextNumber(-randomAngle, randomAngle)
			local offset = CFrame.Angles(x, y, z)
			direction = (CFrame.new(Vector3.zero, BaseDirection) * offset).LookVector
		end

		local Projectile = BaseProjectile:Clone()
		Projectile.Parent = workspace.Effects
		if Projectile:IsA("Model") then
			for _, v in pairs(Projectile:GetDescendants()) do
				if v:IsA("BasePart") then
					v.Anchored = false
					v.CanCollide = false
					v.Massless = true
				end
			end
			if Projectile.PrimaryPart then
				task.defer(function()
					Projectile:PivotTo(CFrame.new(StartPoint))
				end)
				applyProjectilePhysics(Projectile.PrimaryPart, direction, Speed)
			else
				warn("Projectile model has no PrimaryPart")
			end
		else
			task.defer(function()
				Projectile:PivotTo(CFrame.new(StartPoint))
			end)
			Projectile.Anchored = false
			Projectile.CanCollide = false
			Projectile.Massless = true
			applyProjectilePhysics(Projectile, direction, Speed)
		end
		--wait()
		table.insert(projectiles, Projectile)

		-- Auto-destroy after time
		game.Debris:AddItem(Projectile, Time)
	end


	return projectiles
end

return module

if it works it works

this is the where you should had to give attention thats why

Something outside your module is setting the projectile’s CFrame then.

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