How do I add a script into a :Clone() | Is this possible?

I have a clone and I am trying to add scripts into it so it does its pathfinding, but as of right now it just seems to work for a second and then freeze in its place and not work at all.

Here is what I have?
I’m not even sure if this works but from everything I seen on here it does. There are no errors, all the prints work.

--// Variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventsFolder = ReplicatedStorage.Events
local DuplicateTemplate = EventsFolder.DuplicateTemplate

local Template = ReplicatedStorage.Template
local PathfindScriptToCopy = game.ReplicatedStorage.pathfind
local AccelScriptToCopy = game.ReplicatedStorage.Acceleration

local CreateNextbot = script.Parent
local CreateFrame = CreateNextbot.CreateFrame
local createButton = CreateFrame.createButton
local decal_IdBox = CreateFrame.decal_IdBox
local errorMessage = CreateFrame.errorMessage

local setDecalId = nil


--// Main

createButton.MouseButton1Click:Connect(function()
	setDecalId = decal_IdBox.Text
	print("DECAL ID : "..setDecalId)
	
	local NewNextbot = Template:Clone()
	NewNextbot.Name = "New_Nextbot"
	NewNextbot.HumanoidRootPart.Beam.Texture = "http://www.roblox.com/asset/?id="..decal_IdBox.Text
	NewNextbot.Parent = game.Workspace
	print("Created NextBot")
	
	local NewPathfind = PathfindScriptToCopy:Clone()
	NewPathfind.Parent = NewNextbot
	NewPathfind.Disabled = false
	print("Added Pathfinding")
	
	local NewAcceleration = AccelScriptToCopy:Clone()
	NewAcceleration.Parent = NewNextbot
	NewAcceleration.Disabled = false
	print("Added Speed")
	
	print("Created NextBot Successfully!")
end)

put the script inside of the thing you are trying to clone so when you clone it the script will already be in there

Tried that, didnt work.
The script just breaks and doesnt work

Maybe after cloning it, you can enable the script (which means disabling it first)

Why not just clone an instance of the nextbot with all the scripts already inside and just change the texture and disabled values?

Also, you’re accessing the ReplicatedStorage objects differently.

local Template = ReplicatedStorage.Template
local PathfindScriptToCopy = game.ReplicatedStorage.pathfind
local AccelScriptToCopy = game.ReplicatedStorage.Acceleration

This is what I have right now, its currently disabled then I enable it when the parent is set

Just an idea, perhaps use a ModuleScript and instancing each time you create a new bot?

2 Likes

^Fixed this

I tried doing it with the Nextbot with the scripts inside it already but it didn’t work, so I am trying to maybe put them in after and just enable them.

Here is the actual file as of right now if you want to take a peak and try it out.

edit : fixed game file, had an error by accident

Nextbot Devforum.rbxl (53.2 KB)

If you were to use a modulescript, perhaps do something like this?

local PathfindScript = require(ReplicatedStorage.Pathfind)

--// code
local NewPathfind = PathfindScript.new(NewNextbot)
print("Added Pathfinding")

Then in the pathfinding script you can have something along the lines of

local Pathfind = {}
Pathfind.__index = Pathfind

function Pathfind.new(bot)
	local instance = {}
	setmetatable(instance, Pathfind)

	instance.NPC = bot
	instance.h = bot.Humanoid
	instance.folder = bot.Waypoints
	
	return instance
end

function Pathfind:findAndGoTo(destination)
    --// code
end

function Pathfind:getNearest()
    --// code
end

return Pathfind

That should work a dream. If you dont know much about using this method of OOP in lua, I recommend you check out this guide here. Good luck :slight_smile: