I have used PartCache with FastCast and eveything does work, however, I can’t set it up on my own without FastCast. I want a bullet trail (a part) to CFrame with the appropriate Part.Size for a straight raycast from point A to B, and then CFrame back far away after 0.1 seconds. So basically a simple bullet trail. Could someone please provide a small example code?
fast cast parts dont NEED to have bullet drop? just dont add gravity
As I understand, doing a simple raycast from point A to B would be more performative than FastCast. That is why I want to do it that way.
If i were to implement something like this using Part Cache I would probably do something like this:
- precreate your bullet parts using something like local CopiedParts = PartCacheModule.new(YOUR_TEMPLATE_PART, COPIES_AMT)
Few notes here:
- do not use that template part you just made in your main script
- I would highly recommend making a template folder in something like replicated storage
- this is because the partcache args need an instance reference so you cant touch with the original instance without messing it up
- after you make the initial clones, everytime you fire your ray you’re gonna want some code in this spirit
Distance = (Raycast.Position - Origin.Position).magnitude
Direction = (Raycast.Position - Origin.Position)
MiddlePoint = Origin.Position + Direction/2
local BulletClone = PartCacheModule.GetPart(CopiedParts)
BulletClone.CFrame = CFrame.new(MiddlePoint,Raycast.Position) -- We position the Laser at the middle point, facing the raycast position
-- The size is gonna be equal to the distance on the Z axis.
BulletClone.Size = Vector3.new(1,1,Distance)
wait(0.15)
CopiedParts:ReturnPart(BulletClone)
(Some code was copied from this post)
I tried your script and it gave me a “Cannot statically invoke method ‘ReturnPart’ - It is an instance method. Call it on an instance of this class created via PartCache.new” error. Then I changed:
This:
BulletCache.ReturnPart(CachedBullet)
To this:
BulletCache:ReturnPart(CachedBullet)
And it somewhat works, however the trail doesn’t always spawn for some reason. Why?
Also, could you elaborate more on the “Few notes here” part? I don’t quite get that.
Thank you for your help!
Here is my code, I didn’t include all of it for simplicity.
local BulletTemp = Instance.new("Part")
BulletTemp.Anchored = true
BulletTemp.CanCollide = false
local AmmoFolder = workspace:WaitForChild("AmmoFolder")
local BulletCache = ClientPartCach.new(BulletTemplate, 3, AmmoFolder)
local function Shoot(...)
local distance = (MuzzleOrigin - ray.Position).Magnitude
local direction = (ray.Position - Muzzle.Position)
local CachedBullet = ClientPartCach.GetPart(BulletCache)
CachedBullet.CFrame = CFrame.new(Muzzle.Position + direction/2, ray.Position)
CachedBullet.Size = Vector3.new(0.1, 0.1, distance)
task.wait(3)
BulletCache:ReturnPart(CachedBullet)
end
try using a spawn function since youre making it trail for 3 seconds
It CFrames only 3-4 parts and then stops until all of them are gone, and I have 10 parts in the BulletsFolder,
how could you have 10 parts in bullet folder if you only copy 3 parts? if you are shooting alot of parts you need more clones to start right?
It creates new parts when it doesn’t have any, but for some reason still doesn’t always CFrame them when I click. Sometimes it does and sometimes it doesn’t. And I did put a spawn function as you said.
if its ONLY not working when you shoot into the air you have to implement a distance cap for the raycasts
I am standing on one place and shooting a wall right in front of me, there is no way the ray doesn’t hit the wall.
The script seems to work fine when I reduce the wait time to 0.1 secs, however I am still concerned as to why it doesn’t work with higher wait times. Also, I am not sure if it will work if I will test it not with a Handgun but with a Minigun with a much higher fire rate, even with the 0.1 wait time.
thats cause you dont have enough cached parts
the 3 is too low if youre waiting 3 seconds
As I said, when the code doesn’t have enough parts, it spawns new ones so there is no way I have not enough parts. (When I look in the AmmoFolder there are over 10 parts)
ok post ur entire script cause i dont think i have enough info to help
I also added a repeat until to simulate the behavior of a very fast Minigun.
Getting mouse position and ViewRay:
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
repeat
local h = nil--Create this variable just so that "Repeat/until" would work.
local MouseLocation = UserInputService:GetMouseLocation()
local ViewRay = workspace.Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y, 0)
Shoot(ViewRay, Tool.Muzzle)--Call the function and pass the ViewRay to cast a new ray
task.wait(0.1)--Use a wait for the code not to break since it is a loop
until h == "d"
end
end)
Shoot function:
local function Shoot(ViewRay, Muzzle)
spawn(function()
local MuzzleOrigin = Muzzle.Position
if Ammo > 0 and Reloading == false then
local ray = workspace:Raycast(MuzzleOrigin, ViewRay.Direction * 300) --+ Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5)))
local distance = (MuzzleOrigin - ray.Position).Magnitude
local direction = (ray.Position - Muzzle.Position)
local CachedBullet = ClientPartCach.GetPart(BulletCache)
CachedBullet.CFrame = CFrame.new(Muzzle.Position + direction/2, ray.Position)
CachedBullet.Size = Vector3.new(0.1, 0.1, distance)
Ammo -= 1
AmmoGui.Text = Ammo
task.wait(1)
BulletCache:ReturnPart(CachedBullet)
elseif Ammo <= 0 and Reloading == false then
Reload()
end
end)
end
I figured out that this code doesn’t properly work only if I am standing still, but it does work while I am moving. Do you know why?
idk man ima be fr. my guess would be it probably has something todo with your raycast filtering.
local params = RaycastParams.new()
params.FilterDescendantsInstances = {THE_CHARACTER_MODEL} -- this goes through the model's descendants so put the model dir not a part dir
params.FilterType = Enum.RaycastFilterType.Exclude
local raycast = workspace:Raycast(StartVec, ShootVec, params)