CFrame doesn't work after I change the shape of a part (PartCache)

This function fires a Ray and the PartCache Module handles the Bullet Trails, meaning a part that look like a bullet trail will be CFramed as the path of the casted Ray.

The problem is that when I add this:

BulletTemplate.Shape = "Cylinder"

The parts stop CFraming, even though there is no error. Without adding this everything works fine.

Script:

local AmmoFolder = workspace:WaitForChild("AmmoFolder")

local BulletTemplate = Instance.new("Part")
BulletTemplate.Anchored = true
BulletTemplate.CanCollide = false
BulletTemplate.Color = Color3.new(1, 0.835294, 0)
BulletTemplate.Transparency = 0.1
BulletTemplate.Shape = "Cylinder"--Adding this line makes the parts not CFrame, even though without it everything works fine
BulletTemplate.Material = Enum.Material.Neon

local BulletCache = ClientPartCach.new(BulletTemplate, 3, AmmoFolder)

local function ShootBullet(ViewRay, Muzzle)
	local CachedBullet = ClientPartCach.GetPart(BulletCache)
	local MuzzlePosition = Muzzle.Position
	if Ammo > 0 and Reloading == false then
		local ray = workspace:Raycast(MuzzlePosition, ViewRay.Direction * 300) --+ Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5)))
		
if ray ~= nil then
		local RayPosition = ray.Position
		CachedBullet.Size = Vector3.new(0.15, 0.15, (MuzzlePosition - RayPosition).Magnitude)
		CachedBullet.CFrame = CFrame.new(MuzzlePosition + (RayPosition - MuzzlePosition)/2, RayPosition)
		else
			print("Out of range")
			CachedBullet.CFrame = CFrame.lookAt(MuzzlePosition + (ViewRay.Direction * 300)/2, MuzzlePosition + ViewRay.Direction * 300)
			CachedBullet.Size = Vector3.new(0.15, 0.15, 300)
		end

		Ammo -= 1
		AmmoGui.Text = Ammo
		task.wait(0.01)
		BulletCache:ReturnPart(CachedBullet)

	elseif Ammo <= 0 and Reloading == false then
		Reload()
	end
end
1 Like

Have you tried using Enum.PartType?

Also, your .GetPart is wrong, read the documentation for an example.

Code:

local AmmoFolder = workspace:WaitForChild("AmmoFolder")

local BulletTemplate = Instance.new("Part")
BulletTemplate.Anchored = true
BulletTemplate.CanCollide = false
BulletTemplate.Color = Color3.new(1, 0.835294, 0)
BulletTemplate.Transparency = 0.1
BulletTemplate.Shape = Enum.PartType.Cylinder --Adding this line makes the parts not CFrame, even though without it everything works fine
BulletTemplate.Material = Enum.Material.Neon

local BulletCache = ClientPartCach.new(BulletTemplate, 3, AmmoFolder)

local function ShootBullet(ViewRay, Muzzle)
	local MuzzlePosition = Muzzle.Position
	if Ammo > 0 and Reloading == false then
		local CachedBullet = BulletCache:GetPart()
		local ray = workspace:Raycast(MuzzlePosition, ViewRay.Direction * 300) --+ Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5)))

		if ray ~= nil then
			local RayPosition = ray.Position
			CachedBullet.Size = Vector3.new((MuzzlePosition - RayPosition).Magnitude, 0.15, 0.15)
			CachedBullet.CFrame = CFrame.lookAt(MuzzlePosition + (RayPosition - MuzzlePosition)/2, RayPosition) * CFrame.Angles(0, math.rad(90), 0)
		else
			print("Out of range")
			CachedBullet.CFrame = CFrame.lookAt(MuzzlePosition + (ViewRay.Direction * 300)/2, MuzzlePosition + ViewRay.Direction * 300) * CFrame.Angles(0, math.rad(90), 0)
			CachedBullet.Size = Vector3.new(300, 0.15, 0)
		end

		Ammo -= 1
		AmmoGui.Text = Ammo
		task.wait(0.01)
		BulletCache:ReturnPart(CachedBullet)

	elseif Ammo <= 0 and Reloading == false then
		Reload()
	end
end
3 Likes

I did this but it still didn’t work:

BulletTemplate.Shape = Enum.PartType.Cylinder

As for PartCache, if I change the GetPart operator from : to a . it gives me an error and doesnt work. “Cannot statically invoke method ‘GetPart’ - It is an instance method. Call it on an instance of this class created via PartCache.new”

I just copied the code from a guy that helped me set this up on DevHub because I couldn’t set it up myself, and I didn’t find any tutorials for PartCache online.

I didn’t only do that. Look at my code.

Could you try just copying that line?

1 Like

I changed this:

local CachedBullet = ClientPartCach.GetPart(BulletCache)

to this:

local CachedBullet = BulletCache:GetPart()

It still doesn’t work, but no error this time. Did you change anything else in the script that I didn’t see? I don’t want to Ctrl C, Ctrl V the whole code since I have lots of annotations in the main script.

Did you move that line to inside the if statement?

Yes:

if Ammo > 0 and Reloading == false then
		local CachedBullet = BulletCache:GetPart()

I believe you were sizing the part incorrectly, which led to you not being able to see the part. Try my edited code.

1 Like

This did fix the problem, thank you! Could you explain why exactly? My code worked well if I didn’t change the part’s shape, it’s a bit strange for me.

Cylinders are shaped differently in Roblox, and Z vector doesn’t affect its size. It’s easier to see when you change the sizes yourself.

If it all worked, you can set my reply as the solution.

1 Like

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