So I asked about this the other day and didn’t get much help because I didn’t give enough context of the issue. Basically these rats in my game can go and collect cheese from these cheese blocks. I have a for loop that counts down the cheese’s health (which is basically how much is left until it is destroyed) and during that loop the rat collects the cheese. The problem comes up when it is attacking and it shoots out and error saying that “Health” is not part of the cheese model even though I have TWO if statements stating that if health is not there, break the loop. There can be up to 3 rats attacking said cheese and it very rarely spews the error with one rat is collecting the cheese but if there is two or more, it is guaranteed that it will break and make the error I was speaking of.
Here is my code that is in a module script that is running on the server:
function collectCheese(Rat, Cheese, Owner)
Rat.Values.Following.Value = false
local HitTable = {Cheese.CheesePart.Position + Vector3.new(6, 0, 0), Cheese.CheesePart.Position + Vector3.new(0, 0, 6), Cheese.CheesePart.Position + Vector3.new(-6, 0, 0), Cheese.CheesePart.Position + Vector3.new(0, 0, -6)}
local RatPos = HitTable[math.random(1, #HitTable)]
Rat.HumanoidRootPart.BodyPosition.Position = RatPos
wait(1)
for i = Cheese.Health.Value, 1, -1 do
if Cheese:FindFirstChild("Health") then
local CheeseAmount = 0
for i,v in pairs(Owner.PlayerData.Cheese:GetChildren()) do
CheeseAmount += v.Value
end
if Cheese and Cheese.Health and CheeseAmount < Owner.PlayerData.EquippedBackpack.Value then
Rat.HumanoidRootPart.CFrame = CFrame.new(Rat.HumanoidRootPart.Position, Cheese.CheesePart.Position)
Rat.HumanoidRootPart.BodyGyro.CFrame = CFrame.new(Rat.HumanoidRootPart.Position, Cheese.CheesePart.Position)
local Attack = Rat.Humanoid:LoadAnimation(Rat.Attack)
Attack:Play()
PM.GiveCheese(Owner, 1, Owner.PlayerData.Cheese:FindFirstChild(Cheese.Parent.Name))
Cheese.Health.Value -= 1
else
break
end
wait(1.5)
else
break
end
end
end
Any help would be appreciated!