I have a strange problem with my auto firing gun. For some reason, when a player walks into range it will fire off five or so shots, then stop. I am really confused especially because the gun will at times work perfectly.
I know this script looks really long but I basically just create a huge bubble around the gun and when it touches a player it will send a signal to start firing. (most of the actual bullet script is in another script/scripts and that all works fine, just the detection system seem to be malfunctioning sometimes). Here is the script:
ready = true CF = script.Parent.Parent.X.CFrame Target = workspace.Part D = false C = Instance.new("Part") C.Shape = ("Ball") C.Name = ("C") X = 0 C.Size = Vector3.new(400,400,400) C.Anchored = true repeat wait (3) Vehical = script.Parent.Parent.Parent.Parent:FindFirstChild("TeamName") until Vehical ~= nil print ("Joined "..script.Parent.Parent.Parent.Parent:WaitForChild ("TeamName").Value) C.CanCollide = false C.Transparency = 1 ReloadTime = 1 C.Parent = workspace C.Touched:Connect(function(hit) if hit.Name == ("Head") and game.Players:GetPlayerFromCharacter(hit.Parent).Team.Name ~= Vehical.Value and script.Parent.Parent.HP.Value > 0 then print ("Target Aquired:"..(game.Players:GetPlayerFromCharacter(hit.Parent)).Team.Name.. "Entity Detected:Protect".. (Vehical.Value)) X = X + 1 local B = X print("FIGHTING") D = true Target = hit wait (5) if X == B then D = false end end if script.Parent.Parent.HP.Value < 0 then Target = script.Parent.Parent.Gun end end) while wait (ReloadTime) do if script.Parent.Parent.HP.Value < 0 then script.Parent.Parent.X.CFrame = CF end C.Position = script.Parent.Parent.X.Position if D and script.Parent.Parent.HP.Value > 0 then if script.Parent:FindFirstChild("Mounted") and ready and D then ready = false Gun = script.Parent.Configurations ReloadTime = Gun.ReloadTime.Value blue = ((0-ReloadTime) * 100) + 255 Damage = Gun.Damage.Value Speed = Gun.LazerSpeed.Value mounted = Gun.Parent.Mounted.Value end local finish = Target.Position local start = script.Parent.Parent.X Vehical = script.Parent.Parent.Parent.Parent.TeamName wait () local ReplicatedStorage = game:GetService("ReplicatedStorage") local NumberEvent1 = ReplicatedStorage:WaitForChild("CreateNumberEvent1") local Bullet = workspace.ServersideBulletScript2.Bullet:Clone() Bullet.Parent = workspace Bullet.CFrame = start.CFrame local Part = Bullet; Part.CFrame = CFrame.new(Part.Position, finish) Part = start if mounted then Part.CFrame = CFrame.new(Part.Position, finish) script.Parent.Parent.X.CFrame = Bullet.CFrame end Bullet.Anchored = false Bullet.Velocity=Bullet.CFrame.lookVector *100 Bullet.Kill.Disabled = false Bullet.Damage.Value = script.Parent.Configurations.Damage.Value Bullet.Clean.Disabled = false Bullet.Vehical.Value = script.Parent.Parent Bullet.Player.Value = nil local ray = Ray.new(start.Position,(finish-start.Position)*30000) local ignoreList = {Vehical,Bullet} local undecided = true while undecided do wait () undecided = false print (ignoreList) hit, position = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList) print(hit) if hit and hit.Transparency ~= 1 and hit.Name ~= ("Bullet") then elseif hit ~= nil then undecided = true table.insert(ignoreList, hit) end end Bullet.Target.Value = position Bullet.laser.Playing = true wait (ReloadTime) ready = true end end
Any help would be much apreciated.