So I’m making an AI that shoots a gun at you, the whole thing works pretty well. However there’s a bug I’ve come across where whenever the AI finds you and shoots at you, a random mesh appears.
https://gyazo.com/ec453d3f326931e02a2547fcd45cc89b (example)
It vaguely resembles a mesh torso, but that has nothing to do with it
I’ve narrowed it down to being a Issue with cloning a sound over and over whenever the gun is shot.
Also, there are no physical projectiles being created at the moment, only rays. So it wouldn’t be an issue with that. Also whenever I try and select the part ingame it acts as though it is locked, and it doesn’t seem to appear in the workspace which has left me confused.
if gunSettings.CurrentAmmo.Value > 0 then
local root = bot.HumanoidRootPart
local sounds = gun.Main:FindFirstChild("shotsound")
local sound = sounds:Clone()
sound.Parent = gun.Main
local player = game.Players:GetPlayerFromCharacter(target.Parent)
spawn(function()self.visualiser:create(sound,self.tags.danger,gun.Main.Muzzle,player,self.visible)end)
sound:Play()
game.Debris:AddItem(sound,2.5)
local posx = math.random(-5,5)
local posz = math.random(-5,5)
local rayResult = workspace:Raycast(gun.Main.Position,gun.Main.CFrame.LookVector * 200,self.params)
if rayResult then
local human = rayResult.Instance
if human.Parent:FindFirstChild("Humanoid")~= nil then
human.Parent.Humanoid:TakeDamage(gun.settings.Damage.Value)
elseif human.Parent.Parent:FindFirstChild("Humanoid")~= nil then
human.Parent.Parent.Humanoid:TakeDamage(gun.settings.Damage.Value)
end
end
gun.Main.Muzzle.Flash:Emit(1)
gunSettings.CurrentAmmo.Value = gunSettings.CurrentAmmo.Value - 1
wait(60/gunSettings.RPM.Value)
bot.config.shooting.Value = false
end
This is what I think is causing the issue, as when I stop cloning the sound the part stops appearing. Although I can’t do without because my Sound Visualiser relies on individual audios.
Any help is appreciated
(edit: also yes my raycast is broken, I’m fixing it lol)