Alright so I have this waypoint system which allows you to click anywhere upon the ground and the NPC moves to said point which works fine and all but upon clicking the ground, a part is created at that position and once the NPC reaches the position it should delete the part which it does but only the first time. So I need help on making it delete the part after it reaches it every time. Any help is appreciated!
![]()
(“WaypointSetter” gets a V3 value and sends it to “WaypointHandler” that changes the hard V3 value I have to the new pos. where the “MovementScript” then moves the character to the point and upon reaching the point it fires an event that “WaypointSetter” picks up on and deletes the part.)
Here are all of the Scripts -
(WaypointSetter)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local BF = game.Workspace.BV_Testing.Battlefield
local MTEvent = game.ReplicatedStorage.MoveToEvent
local MEvent = game.ReplicatedStorage.MovedEvent
local Cooldown = false
local function onMouseClick()
if mouse.Target.Parent.Name == "Lands" and not Cooldown then
Cooldown = true
Waypoint = Instance.new("Part")
Waypoint.Anchored = true
Waypoint.Size = Vector3.new(1,1,1)
Waypoint.Position = mouse.Hit.p
Waypoint.Name = "AxisWaypoint"
Waypoint.CanCollide = false
Waypoint.Parent = game.Workspace
MTEvent:FireServer(Waypoint.Position, true)
end
end
MEvent.OnClientEvent:Connect(function(Plr)
Waypoint:Destroy()
Cooldown = false
end)
mouse.Button1Down:Connect(onMouseClick)
(WaypointHandler)
local MTEvent = game.ReplicatedStorage.MoveToEvent
local WP = game.Workspace.BV_Testing.Framework.WaypointValues.AxisWaypoint
MTEvent.OnServerEvent:Connect(function(Plr, Pos)
WP.Value = Pos
end)
(MovementScript)
local MTEvent = game:GetService("ReplicatedStorage").MoveToEvent
local MEvent = game:GetService("ReplicatedStorage").MovedEvent
local Human = script.Parent.Humanoid
local targetReached = false
MTEvent.OnServerEvent:Connect(function(Plr, Pos, andThen)
function andThen()
MEvent:FireClient(Plr)
end
local connection
connection = Human.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen()
end
end)
Human:MoveTo(Pos)
spawn(function()
while not targetReached do
if not (Human and Human.Parent) then
break
end
if Human.WalkToPoint ~= Pos then
break
end
Human:MoveTo(Pos)
wait(6)
end
if connection then
connection:Disconnect()
connection = nil
end
end)
end)
(link to the test place)