So why not use Part:Destroy(). Other than using
Debris:AddItem(Part, 1)?
So why not use Part:Destroy(). Other than using
Debris:AddItem(Part, 1)?
Debris service makes blocks move if it has velocity (and destroy). :Destroy() just destroys a instance
It is the exact same as :Destroy() function but Debris is quite handy because of how simple it is
-- Messy and complicated
delay(3, function()
if projectile and projectile.Parent then
projectile:Destroy()
end
end)
-- Simple
Debris:AddItem(Part, 3)
Both of these are the exact same but using Debris will reduce a lot of messy and unnecessary line of codes.
Check it out.
Because Debris can add the object for a certain amount of time
local part = Instance.new("Part",workspace)--create a part
game.Debris:AddItem(part,5)--the part will exist for 5 seconds before it destroys
You never use Debris.
Yeah, you would use it when you want a item out of the game in a certain amount of time:
local Debris = game:GetService("Debris")
local Folder = Instance.new("Folder")
Folder.Parent = workspace
for i, v in pairs(Folder:GetChildren()) do
Debris:AddItem(v, 2) -- v would be the items in the folder, and the 2 means how long until it get deleted.
end
because Debris:AddItem is like adding something with an expiration date
for example, if u shoot a bullet, u wouldnt just destroy the bullet 2 seconds later every time a bullet is shot when u can just use debris:additem
Why not just use delay or wait(2) if you wanted to stop the bullet being destroyed?
It’s shorter I guess? It also is ran on a “new thread” aka your code wont yield.
You should not use delay, or Debris. Both are bad!
What is the alternatives to delay debris utf8 etc?
Custom implementations. Not sure what relevance the utf8
library brings though
I still use it and people say not to that is
utf8
library has nothing to do with this here. Implementing a custom wait is trivial:
local RunService = game:GetService("RunService")
local function wait(n: number): number
local dt = 0
while dt < n do
dt += RunService.Heartbeat:Wait()
end
return dt
end
A custom delay
is also trivial to implement.
-- might as well implement a custom spawn too
local function spawn(f: () -> ())
local event = Instance.new("BindableEvent")
event.Event:Connect(f)
event:Fire()
event:Destroy()
end
local function delay(n: number, f: (number?) -> ())
spawn(function()
f(wait(n)) -- using the custom wait from earlier
end)
end
It’s trivial to you maybe but hard for me like how can you even use : in a functions arguments?
It’s something that Luau uses to specify specific data types with its methods. Think of it similar to how you define a variable in C/C++/C# with int
, string
and etc.
I sometimes use Debris:AddItem
when I want to destroy an object after a bit, for example when I fling people.
local FlingForce = Instance.new("BodyVelocity");
Debris:AddItem(FlingForce, .1); -- Destroy after 1/10th of a second
FlingForce.Velocity = Vector3.new(...);
FlingForce.Parent = Torso;
If you wanted to, you could use my simple debris
module - it allows you to add, overwrite or remove an item’s lifetime in queue. The max items it can hold can also be changed (its default is 1500, the actual Debris service’s default). I wont comment on delay
or utf8
'cuz incapaz covered that already.
@sjr04 is using typed-lua. Probably not the best idea when trying to show example code when helping people. Luau Type Checking Beta!
local RunService = game:GetService("RunService")
local function wait(n)
local dt = 0
while dt < n do
dt += RunService.Heartbeat:Wait()
end
return dt
end
local function spawn(f)
local event = Instance.new("BindableEvent")
event.Event:Connect(f)
event:Fire()
event:Destroy()
end
local function delay(n, f)
spawn(function()
f(wait(n)) -- using the custom wait from earlier
end)
end
Debris is useful for destroying parts at a later time, without yielding your current thread.
For example if we did:
Wait(2) – this yields the thread
Part:Destroy()
print ‘lol’ – this only runs after the part is destroyed and the wait(2) is over, but what if we don’t want to yield the thread while destroying the part at a later time?
It fails in even that though since debris uses spawn() which has an inbuilt wait() before spawning a new thread, and wait() usually waits more then it’s supposed to.
Instead you could simulate what debris does this way (with more cooler stuff offcourse) without relying on it’s spawn behavior by using RunService.Heartbeat:
function AddItem(Part, Time, CallBack, OnCancel) -- CallBack ONLY runs when AddItem destorys the item, not if the user destorys it
local State = {Cancel = false}
local Elapsed_Time = 0
local Connection
-- ill be using a psuedo thread system with .Heartbeat:Connect instead of creating an actual thread
Connection = game:GetService('RunService').Heartbeat:Connect(function(T)
Elapsed_Time += T
if State.Cancel == true then
Connection:Disconnect()
if OnCancel then
coroutine.wrap(OnCancel)(Elapsed_Time)
end
return
elseif Part == nil then
Connection:Disconnect()
return
elseif Elapsed_Time >= Time then
Connection:Disconnect()
Part:Destroy()
if CallBack then
coroutine.wrap(CallBack)(Elapsed_Time)
end
return
end
end)
return State
end
local State = AddItem(
workspace.Baseplate,
5,
function(TotalTimeTaken)
print(TotalTimeTaken)
print 'LOL '
end,
function(totalTimeTaken)
print ('haha we made debris quit', " ", totalTimeTaken)
end
)
wait(2)
State.Cancel = true