Hello, i have created a gun with a building system in my game.
When the gun shoots a building 4 times, it drains 0.50 hp from the building every shot. When its been shot 4 times it breaks, but on the fourth shot it fires an error. I believe this is because its trying to index it after its been destroyed, but im not quite sure.
I’m not really sure what to do, so please tell me if you have a soloution.
ps: (its my first time posting so sorry if theres any confusion)
if raycastresult then
body.Parent = bullet
bullet.Parent = game.Workspace.hi
bullet.Touched:Connect(function(oP)
if oP.Parent == game.Workspace then
bullet:Destroy()
end
end)
if raycastresult.Instance.Name == "building" then
raycastresult.Instance.hp.Value -= 0.50
raycastresult.Instance.Transparency += 0.25
if raycastresult.Instance.hp.Value <= 0 then
raycastresult.Instance:Destroy()
end
end
if raycastresult.Instance.Parent.Name ~= owner then
local humanoid = raycastresult.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local debris = game.ReplicatedStorage.debris:Clone()
if raycastresult.Instance.Name == "Head" then
dmg = 40
debris.Color = Color3.new(1, 0, 0)
end
debris.Parent = humanoid.Parent.Torso
debris.Position = humanoid.Parent.Torso.Position
local goal = {}
goal.Size = Vector3.new(6,6,6)
local ti = TweenInfo.new(0.5)
local tween = game.TweenService:Create(debris,ti,goal)
humanoid.Health -= dmg
dmg = 20
tween:Play()
local secgoal = {}
secgoal.Size = Vector3.new(0.1,0.1,0.1)
wait(0.1)
db = false
wait(0.5)
local tween2 = game.TweenService:Create(debris,ti,secgoal)
tween2:Play()
wait(0.5)
debris:Destroy()
end
end
Thanks!