I want to remove the bullet after 3 seconds for example, if I shoot at the sky, the bullet will never hit anything, so it will never destroy
(I’m using the fastcast module)(sorry for bad english)
function onrayhit(cast,result,velocity,bullet)
local hit = result.Instance
if hit then
task.wait(0.01)
bullet:Destroy()
else
task.spawn(function()
task.wait(3)
bullet:Destroy()
end)
end
end
I’m not entirely sure how FastCast works, however it seems that your onrayhit function is only firing when the ray has actually hit something, which means your if else statement does not do anything, you’ll need to look into the FastCast documentation however I’m assuming it’s as simple as firing the Ray and deleting it after 3 seconds.
I just want to point out that there is actually a service named DebrisService. This service allows the developer to schedule the removal of the object without yielding any code, through the usage of the Debris:AddItem() method.
After the lifetime argument has elapsed (in seconds) the object is removed in the same manner as Instance:Destroy().
Instead of:
task.wait(3)
bullet:Destroy()
You can use DebrisService:AddItem() like so:
game:GetService("DebrisService"):AddItem(bullet, 3)
-- The first argument is the object you want to remove and the second argument is the delay (in seconds) at which the object gets removed from the game