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

[Solved] 14:59:52.303 - ServerScriptService.Load:9: attempt to index nil with 'Clone' - Help?

Problem: Model wont clone into game.Workspace.Blocks

I have a part saving system, here is the save script:

local DSS = game:GetService("DataStoreService")
local modelDataStore = DSS:GetDataStore("Models")

game.Players.PlayerAdded:Connect(function(player)
	local models = {}
	for _, model in pairs(game.Workspace.Blocks:GetChildren()) do
		local modelData = {}		
		local primaryPart = model.PrimaryPart
		local position = primaryPart.Position
		local direction = primaryPart.CFrame.lookVector
		modelData.ModelName = model.Name
		modelData.Position = {position.X, position.Y, position.Z, direction.X, direction.Y, direction.Z}
		models[#models + 1] = modelData
		print(models.ModelName)
	end
	modelDataStore:SetAsync(player.UserId, models)
	print('done')
end)

There is no errors on the save script, but on the load script it fails to load in.

local DSS = game:GetService("DataStoreService")
local modelDataStore = DSS:GetDataStore("Models")

game.Players.PlayerAdded:Connect(function(player)
	local models = modelDataStore:GetAsync(player.UserId)
	for _, modelData in pairs(models) do
	local targetModel = modelData.ModelName
	local information = modelData.Position
	local newModel = game.ReplicatedStorage:FindFirstChild(targetModel):Clone() --Script errors here.
	local position = Vector3.new(information[1], information[2], information[3])
	local lookVector = Vector3.new(information[4], information[5], information[6])
	newModel:SetPrimaryPartCFrame(CFrame.new(position, position + (lookVector * 5)))
	newModel.Parent = game.Workspace.Blocks
end
end)

Not sure if this is a problem with data not saving or whatever. Thoughts? Thanks, JailBreaker_13

4 comments

0

Try storing game.ReplicatedStorage:FindFirstChild(targetModel) in a value and then clone that value.

0

Sorry.. I dont follow lol, could you explain it simpler?

0

Like this: "local variable = game.ReplicatedStorage:FindFirstChild(targetModel)" Next line: "local newModel = variable:Clone()" if you want you could print variable to see what is in it. The value's name could be whatever you want obviously.

0

Worked! Want to lock the question? Lots of people have asked this question before.

1 answer

Thanks to Benbebop, the question was solved. I just had to try a different route.

The error was in the load script, it did not want to clone, but I just tried a different route and it worked!

Again thank Benbebop for solving the question!

The error was on line 11 where it gets cloned, I had to set what to clone as a separate variable and it worked.

local DSS = game:GetService("DataStoreService")
local modelDataStore = DSS:GetDataStore("Models")

game.Players.PlayerAdded:Connect(function(player)
	local models = modelDataStore:GetAsync(player.UserId)
	for _, modelData in pairs(models) do
	local targetModel = modelData.ModelName
	local information = modelData.Position
		local variable = game.ReplicatedStorage:FindFirstChild(targetModel)
		print(variable)
	local newModel = variable:Clone()
	local position = Vector3.new(information[1], information[2], information[3])
	local lookVector = Vector3.new(information[4], information[5], information[6])
	newModel:SetPrimaryPartCFrame(CFrame.new(position, position + (lookVector * 5)))
	newModel.Parent = game.Workspace.Blocks
end
end)

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