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

How do I check if an object is clonable?

I'm making an experimental script that clones everything in the workspace, but the thing is, is that it stops whenever it encounters something it can't clone, E.G Terrain.

11:28:30.922  Camera  -  Server - Script:5
11:28:30.923  Baseplate  -  Server - Script:5
11:28:30.923  Cannot Clone() Terrain

1 answer

✓ Accepted answer

You can use pcall to run a function and if it errors then it's not cloneable:

local function Clone(Object)
	local Success, Response = pcall(Object.Clone, Object)

	if Success then
		return Response
	else
		return nil, Response
	end
end

Clone(workspace.Baseplate) -- Baseplate
Clone(workspace.Terrain) -- nil, Cannot Clone() Terrain

[ Success ] is a boolean which returns if the called function has ran successfully, if so, it returns the [ Response ] which in case of success is the cloned Instance. If cloning fails, however, it returns nil as nothing was cloned and the [ Response ] as second return value, in case of error it refers to the error message, string. Take in mind that if Instances's [ .Archivable ] property is set to false, the Instance cannot be cloned and will always return nil.

This pcall is called with 2 arguments, reason for that is because you normally call [ :Clone() ] with a colon (:) and in this case it's impossible so you need to pass the extra self argument manually.

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