I'm making sort of a "special attack". When you mine one of the grass blocks than the one in front of that grass block should be removed as well.
Everything works right now but it searches through hundreds of models to look for one model that has the magnitude of 8 to the real grass you are mining so it lags a lot.
Is there any better way to make this function better and not as laggy.
Modulescript:
local Attacks = {}
function Attacks:SpecialAttack(player, self, zone)
local Tool = player:WaitForChild("Tool")
local k = 0
local ChosenGrass
local ChosenIndex
if Tool.Value == "Trident" then
for i,v in pairs(workspace.Harvestable.Grass[zone.Name]:GetChildren()) do
ChosenIndex = math.random(1, #workspace.Harvestable.Grass[zone.Name]:GetChildren())
ChosenGrass = workspace.Harvestable.Grass[zone.Name]:GetChildren()[ChosenIndex]
if (ChosenGrass.PrimaryPart) and ChosenGrass ~= self.NPC and (ChosenGrass.PrimaryPart.Position - self.NPC.PrimaryPart.Position).magnitude < 8 then
return ChosenGrass
end
end
end
end
return Attacks

3 comments
You could shoot a Ray to the front of the Grass and find the part using :FindPartOnRay().
Oh okay, Thanks for the answer.
Like Precise said try to use a Ray that will start from the part you hit and extend the ray 8 units and if you find a part thats another grass part remove that too