I'm making a power up, you shoot a "Fireball" out of the player and into the workspace. I want to see how I could make it so that when the "Fireball hits something it explodes, but after 60 seconds if the "Fireball" didn't hit anything it just is destroyed. And also on how to make a cool down. I've tried these things out but they didn't work.
Here's the script
local Plr = game.Players.LocalPlayer local Mouse = Plr:GetMouse() local Ammo = 1 Mouse.KeyDown:connect(function(key) key = key:lower() if key =="f" then print("F has been pressed!") local FireBall = Instance.new("Part") FireBall.Shape = "Ball" FireBall.BrickColor = BrickColor.new("Maroon") FireBall.Transparency = 0.5 FireBall.TopSurface = "Smooth" FireBall.BottomSurface = "Smooth" local Fire = Instance.new("Fire") Fire.Parent = FireBall FireBall.CFrame = Plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-6) local BodyVelocity = Instance.new("BodyVelocity") BodyVelocity.Velocity = Plr.Character.HumanoidRootPart.CFrame.LookVector * 90 BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) BodyVelocity.Parent = FireBall local Explosion = Instance.new("Explosion") FireBall.Parent = workspace Ammo = Ammo - 1 wait(3) Ammo = 1 wait(1) FireBall:Destroy() end end)
What I will do is create a touched event and destroy the fireball if it touches something (you can add your own code to deal with what to do with the hit in there) I will then add the fireball to the debris service so that it will only last for 60 seconds Afterwards it will wait for the fireball to be destroyed and then replenish the ammo variable.
I kept the cooldown of 3 seconds there.
local Plr = game.Players.LocalPlayer local Mouse = Plr:GetMouse() local Ammo = 1 Mouse.KeyDown:connect(function(key) key = key:lower() if key =="f" then print("F has been pressed!") local FireBall = Instance.new("Part") FireBall.Shape = "Ball" FireBall.BrickColor = BrickColor.new("Maroon") FireBall.Transparency = 0.5 FireBall.TopSurface = "Smooth" FireBall.BottomSurface = "Smooth" local Fire = Instance.new("Fire") Fire.Parent = FireBall FireBall.CFrame = Plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,-6) local BodyVelocity = Instance.new("BodyVelocity") BodyVelocity.Velocity = Plr.Character.HumanoidRootPart.CFrame.LookVector * 90 BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) BodyVelocity.Parent = FireBall local Explosion = Instance.new("Explosion") --create touched event FireBall.Touched:Connect(function(hit) wait() FireBall:Destroy() end) FireBall.Parent = workspace --Add Fireball to debris (will be destroyed after 60 seconds) game:GetService("Debris"):AddItem(FireBall,60) Ammo = Ammo - 1 --wait for fireball to be destroyed while FireBall.Parent do wait() end --cooldown wait(3) Ammo = 1 end end)