Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

?Every time when ai is clone its dont work

Every time when ai clone in workspace it broke. If ai already in workspace then no broken

ai script

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local path = PathfindingService:CreatePath({
	AgentCanJump = false
})

local npc = script.Parent
local humanoid = npc.Humanoid

local TEST_DESTINATION = game.Workspace.A
local Reset = game.Workspace.AA
local Dead = game.Workspace.exit2

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local c

local function followPath(destination)
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(npc.HumanoidRootPart.Position, destination.Position)
		wait(3)
		path:ComputeAsync(npc.HumanoidRootPart.Position, destination.Position)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()

		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				followPath(destination)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not computed!", errorMessage)
	end
end


followPath(TEST_DESTINATION)

c = false
npc.HumanoidRootPart.ProximityPrompt.Triggered:Connect(function()
	c = true
	local a = math.random(5,15)
	game.ReplicatedStorage.stats.bank.Value = game.ReplicatedStorage.stats.bank.Value + a
	while wait(0.03) do
	humanoid.WalkSpeed = 16
	followPath(Reset)
end
end)

while wait() do
if c == false then
	wait(60)
		followPath(Dead)
	else 
		if c == true then
	break
	end
	end	
end

clone script

main = game.ReplicatedStorage.NPC
while wait(20) do
	    clone = main:Clone()
		clone.Parent = game.Workspace
		clone:SetPrimaryPartCFrame(CFrame.new(-9.858, 6, -11.149))
	end

1 answer

Try changing 'main' of the clone script to this:

main = game:GetService("ReplicatedStorage").NPC

This discussion is preserved as it was. New answers, comments, and votes are closed.