Hey, devs!
Sorry that this post is long, but…
The last week or two, I have been developing an “assist mode” for players who want it. One of the features is to use PathfindingService to create a path, then create parts on every waypoint to draw to the location specified. Everything works fine up until destroying the old waypoints.
The idea is when you are moving, delete old parts, generate new parts to the end of the map or whatever other location on every waypoint, and that’s it. Like I said, it all works, but old parts don’t destroy.
Here is a screenshot of what happens:
And as you could imagine, it generates so many parts until studio just freezes or crashes.
What it does is when it generates the path and parts, it puts the parts into a table that gets returned to the script calling the function. I use a for loop to loop through them all and destroy them.
I have tried using :Remove()
instead of :Destroy()
, I’ve tried setting the parent of the part to nil manually instead of :Remove()
and :Destroy()
, when I print the table, all of the parts are in the table, when I print the part itself, it prints the name of the part (like you would expect), when I print typeof(Part)
, it prints “Instance,” I created a function built into a system to clear every part with the name “WaypointPart” and called that right before the new parts generate (all that does is make it so the parts don’t generate until after the player stops moving), and nothing has worked.
Here is the code relevant to this:
Main server script:
local runService = game:GetService("RunService")
local previousWaypoints
runService.Heartbeat:Connect(function()
if assistMode.IsEnabled(player) then
if character.Humanoid.MoveDirection.Magnitude > 0 then
if assistMode.PriorityPart then
if previousWaypoints then
for _, previousWaypoint in previousWaypoints do
previousWaypoint:Destroy()
end
end
previousWaypoints = assistMode:DrawPathToPriority()
end
end
end
end)
The Assist Mode module:
local pathfindingService = game:GetService("PathfindingService")
local module = {}
module.PriorityPart = workspace:WaitForChild("TestPart")
function module:DrawPathToPriority()
local path = module.CreatePathToEnd()
local waypointParts = {}
if path and path.Status ~= Enum.PathStatus.NoPath then
for _, waypoint: PathWaypoint in path:GetWaypoints() do
local a = create.WaypointPart(waypoint.Position)
table.insert(waypointParts, a)
end
end
return waypointParts
end
function module.CreatePathToEnd(): Path
local path
if module.PriorityPart then
path = pathfindingService:FindPathAsync(Players:GetPlayers()[1].Character.HumanoidRootPart.Position, module.PriorityPart.Position)
end
return path
end
return module
And the CreateInstance module:
function module.WaypointPart(position: Vector3)
local newPart = Instance.new("Part")
newPart.Name = "WaypointPart"
newPart.Size = Vector3.new(0.4, 0.4, 0.4)
newPart.Anchored = true
newPart.CanCollide = false
newPart.Transparency = 0.5
newPart.Material = Enum.Material.Neon
newPart.BrickColor = BrickColor.new("Bright yellow")
newPart.Shape = Enum.PartType.Ball
newPart.Position = position - Vector3.new(0, 0.45, 0)
newPart.Parent = workspace.FX
return newPart
end
Any and all help is appreciated. Thank you!
Best regards,
Amora