Need help with my code

Hey,
So I’m making a smokescreen system for my tank which on pressing a key deploys the smokescreen projectiles, and when the debounce time is met, deletes the projectiles, so my problem is that when I’m outside of the tank, not sitting in the tank and after deploying the smoke screen, it no longer goes away even after waiting through the entire debounce time, but it goes away when I am in the tank, it make’s me think that when I’m outside the tank, the script no longer has control of running without a player in it to execute its functions, so maybe running it in background would work? Idk, I’m not sure I tried a lot but nothing seem’s to work so I thought I could ask here for help…

Here’s my code if anyone would like to help…

Cant you just do this?

game:GetService("Debris"):AddItem(projectile1, 20)
game:GetService("Debris"):AddItem(projectile2, 20)

Roblox has a built in service called “Debris”. This is for if you want maybe bullet shells to fly out of a gun, then deleting itself 8 seconds later.

Debris doesnt yield any code, which means it is right now the easiest way to delete extra items.

EDIT:
Just realized that “projectile1” isnt a variable. Its for a descendant instance. In that case, try this code:

local partTable = {}

for _, projectile1 in ipairs(Turret.smokeLauncher1:GetChildren()) do
	if projectile1:IsA("Part") and projectile1:FindFirstChild("smog") then
		table.insert(partTable, projectile1)
	end
end

for _, projectile2 in ipairs(Turret.smokeLauncher2:GetChildren()) do
	if projectile2:IsA("Part") and projectile2:FindFirstChild("smog") then
		table.insert(partTable, projectile2)
	end
end

game:GetService("Debris"):AddItem(partTable[1], 20)
game:GetService("Debris"):AddItem(partTable[2], 20)
1 Like

Uhh sorry but where am I supposed to place this?

Isnt it obvious? Where you want to delete the smoke grenade

Hello, Forgot to mention this. Remember to set the partTable to nil at the end of your code. This is to avoid any future problems and making multiple tables could take up a lot of space.

1 Like

Thank you, hope this should be better.


for _, projectile1 in ipairs(Turret.smokeLauncher1:GetChildren()) do
	if projectile1:IsA("Part") and projectile1:FindFirstChild("smog") then
		table.insert(partTable, projectile1)
	end
end

for _, projectile2 in ipairs(Turret.smokeLauncher2:GetChildren()) do
	if projectile2:IsA("Part") and projectile2:FindFirstChild("smog") then
		table.insert(partTable, projectile2)
	end
end

game:GetService("Debris"):AddItem(partTable[1], 20)
game:GetService("Debris"):AddItem(partTable[2], 20)

partTable = nil

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