I am making a script that makes a shotgun function; this includes the bullets firing, flame, sounds, and light.
However, while the bullet firing works, the number of bullets duplicate each time another shotgun is present and causes the cooldown to go to every other shotgun.
Can someone demonstrate what the problem is so I can work from there?
robloxapp-20240728-1412013.wmv (1.1 MB)
local shootEvent = game.ReplicatedStorage.ShootEvent
local Debris = game:GetService(“Debris”)
local cooldownTime = 0.7 – time in seconds between shots
local canShoot = true – variable to track if the gun can shoot
local function OnActivated(player, gun)
if not canShoot then return end – if the gun is on cooldown, don’t shoot
canShoot = false – put the gun on cooldown
local GTR = gun:FindFirstChild("GunTubeRight")
local GTL = gun:FindFirstChild("GunTubeLeft")
local FirepartL = gun:FindFirstChild("FirePartL")
local FirepartR = gun:FindFirstChild("FirePartR")
local bulletl = gun:FindFirstChild("BulletL")
local bulletr = gun:FindFirstChild("BulletR")
local fireRight = Instance.new("Fire")
fireRight.Parent = FirepartR
fireRight.Color = Color3.new(1, 1, 0.498039)
fireRight.Heat = 3
fireRight.SecondaryColor = Color3.new(1, 1, 0)
fireRight.Size = 3
local fireLeft = Instance.new("Fire")
fireLeft.Parent = FirepartL
fireLeft.Color = Color3.new(1, 1, 0.498039)
fireLeft.Heat = 3
fireLeft.SecondaryColor = Color3.new(1, 1, 0)
fireLeft.Size = 3
local light = Instance.new("SpotLight")
light.Parent = FirepartL
light.Parent = FirepartR
light.Color = Color3.new(1, 1, 0.498039)
light.Brightness = 10
light.Range = 20
light.Shadows = true
light.Face = "Right"
local sound = Instance.new("Sound")
sound.Parent = gun:FindFirstChild("TubeHolders")
sound.SoundId = "rbxassetid://1905367471"
sound.MinDistance = 20
sound.MaxDistance = 50
sound.TimePosition = 0
sound.Volume = 2
sound:Play()
if sound.TimePosition == 1 then
sound:Stop()
end
local newBulletR = bulletr:Clone()
local newBulletL = bulletl:Clone()
newBulletR.Parent = workspace
newBulletL.Parent = workspace
newBulletR.CFrame = gun.GunTubeRight.CFrame
newBulletL.CFrame = gun.GunTubeLeft.CFrame
local directionl = gun.GunTubeLeft.CFrame.RightVector
local directionr = gun.GunTubeRight.CFrame.RightVector
local bodyVelocityR = Instance.new("BodyVelocity")
bodyVelocityR.Velocity = directionr * 100
bodyVelocityR.Parent = newBulletR
local bodyVelocityL = Instance.new("BodyVelocity")
bodyVelocityL.Velocity = directionl * 100
bodyVelocityL.Parent = newBulletL
newBulletR.Anchored = false
newBulletL.Anchored = false
wait(0.1)
local reload = Instance.new("Sound")
reload.Parent = gun:FindFirstChild("TubeHolders")
reload.SoundId = "rbxassetid://2304316637"
reload.MinDistance = 20
reload.MaxDistance = 50
reload.Volume = 2
reload:Play()
if reload.TimePosition == 1 then
reload:Stop()
end
local function onBulletHit(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(25) --The amount of damage you want to do
end
end
newBulletR.Touched:Connect(onBulletHit) -- connects the Touched event to the onBulletHit function
newBulletL.Touched:Connect(onBulletHit) -- connects the Touched event to the onBulletHit function
wait(0.5)
fireRight:Destroy()
fireLeft:Destroy()
light:Destroy()
wait(1)
Debris:AddItem(newBulletR,1)
Debris:AddItem(newBulletL,1)
wait(cooldownTime) -- wait for the cooldown time
canShoot = true -- takes the gun off cooldown
end
shootEvent.OnServerEvent:Connect(OnActivated)