I making a laser from one point to another which works but after some seconds I want to destroy it but its not destroying
heres my client script
the code is inside a remote event and the script in startplayerscripts(at first it was inside the tool but its in here because I thought it would fix the problem but nope)
local shootPoint = tool.Handle.ShootPoint
tool.Activated:Connect(function()
local mousePos = game.Players.LocalPlayer:GetMouse().Hit.Position
local origin = shootPoint.WorldPosition
local direction = CFrame.lookAt(origin, mousePos).LookVector * 100
local distance = (origin - mousePos).Magnitude
local midpoint = (origin + mousePos) / 2
local part = Instance.new('Part')
part.Name = 'Laser'
part.CanCollide = false
part.Anchored = true
part.Material = 'Neon'
part.Transparency = 0.5
part.Size = Vector3.new(0.5, 0.5, distance)
part.CFrame = CFrame.lookAt(midpoint, mousePos)
part.Color = Color3.fromRGB(14, 255, 22)
part.Parent = tool
task.wait(0.3)
part:Destroy()
while task.wait(1) do
print('Running')
part:Destroy()
print(part)
print(part.Parent)
end
end)
so im creating the part, waiting 0.3 seconds and then destroying the part but its not destroying
I have a while loop that destroys it every second but some how it still exists because it still prints, but printing part.Parent is nil
use debris service then see if it does the same thing, i think i had this issue and just switch to debris service, its also a lot faster than manually deleting a part
local Debris = game:GetService("Debris")
Debris:AddItem(part, 0.3) -- 0.3 is how long you want it to be there for
Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections, and calls Destroy on all children. This function is the correct way to dispose of objects that are no longer required.
Destroyed objects still technically exist until they get ‘garbage collected’. So long as you have a reference to the destroyed object (usually through a local variable), the object will remain in memory. As soon as all references to a destroyed object are gone (for example when your variable referencing the object goes out of scope), they are marked for clean-up. Then, the ‘garbage collector’ will remove the data at some unspecified point in the near future.
This is all very technical and complicated, but the basic idea is that calling :Destroy() will prepare the object for deletion, rather than fully cleaning it up right then and there. You cannot interact with a destroyed object and so long as you’re not storing references to destroyed objects, the object will eventually disappear. There is nothing to worry about here.