Model cloned into Replicated Storage cannot change parent, returns nil

I have a local script that will call InvokeServer() to clone an item from server storage and place it into replicated storage. From there, the cloned item is returned and should be used by the code snippet below to change its parent to a viewport frame.
This code was working flawlessly all day until just now, where it will now error out when parenting the cloned item from replicated storage to the viewport frame.

Local Script

local itemprev = itempreview:InvokeServer(i,key)

itemprev.Parent = itempanel -- Returns "attempt to index nil for 'parent'" error.

Server Script

itempreview.OnServerInvoke = function(plr,id,subid)
	print("cloning preview - start")
	local succ,err = pcall(function()
		for key,value in pairs(itemlist) do
			if key == id then
				print("cloning preview - id matches keyid")
				for _,v in pairs(serverstorage:GetDescendants()) do
					--print(v.Name,value.Name)
					if v:IsA("Folder") and string.match(v.Name,value.Name) then
						print("cloning preview - found: "..tostring(v))
						for _,w in v:WaitForChild("preview"):GetChildren() do
							if w:IsA("Folder") and string.match(w.Name,subid) then
								print("cloning preview - found toggle #"..subid)
								for _,x in w:GetChildren() do
									if x:IsA("Model") then
										print("cloning preview - found "..x.Name..", cloning now")
										local clone = x:Clone()
										clone.Parent = repstorage.preview
										return clone
									end
								--break
								end
							end
						end
					break
					end
				end
			break
			end
		end
	end)
	if not succ then warn(err) end
end

All I know right now is that the server script portion is not the issue here and this was working flawlessly prior to me opening Roblox Studio like 2 hours ago as of this post.

I have no idea if this is an actual issue with my code SOMEHOW or if it’s a new bug that I’m dealing with-

Please add

print(clone.Parent)

just before

return clone 

and see what it prints

It’s parented to the proper folder I asked it to be in.
image
Still complains about the parent being nil when it shouldn’t.

Like I said, the server portion is completely fine and I never touched this script until today. I have no clue what’s going on now and I have a strong feeling this is a new bug.

Okay try returning clone.Name and then print it on the client side script

‘Attempt to index nil with Name’ now. Except the cloned item does exist, so idk what the game is considering nil to be.

Okay I’m reading some of documentation on remote functions, it’s possible that the clone instance didn’t have time to replicate to client and if that’s the case, the client receives nil instead of the instance. Try adding task.wait(2) or something like that before returning clone and see if that fixes it, if it does then at least we know what the issue is.

Also I’m not a big fan of remote functions so I suggest scraping it all together, use a remote event instead and add a ChildAdded listener to the preview folder.

I added task.wait(5) before the return line, issue is still persisting.

I shouldn’t have to change the code around when this code was working hours ago. I’m gonna make a bug report on this and see if the roblox devs flipped a switch or something cause this is wild.

Honestly your code is incredibly messy with so many nested ifs and fors that I can barely wrap my head around it, I don’t think the right choice here is to file a bug report when your own code is suboptimal.

Again, the code was working. Whether or not it’s messy or not on par with someone advanced in lua is not relevant at all.

What matters in the code is that I cloned a model from server storage to replicated storage, then moved that model from replicated storage to a viewport frame. This was working for weeks on end until today.

All I did between some hours ago and now was adding values to a module script. Didn’t touch this part of my code at all and it’s now broken for absolutely no reason. This sounds like a bug to me considering there’s nothing on this issue anywhere else on the forums nor online.

Roblox Studio is not perfect, it could easily not save some of your changes to that script that made it work earlier, it’s just as likely that it rolled back the script to a version where it’s not working. The fact you refuse to test solely because “it worked before therefor it has to work now” is beyond me, and even go as far as to claim it’s a Roblox bug.

If anything, this should be an indicator to you that something is wrong with your code and it’s not an engine bug that happens to multiple people.

And lastly, to further prove my point this is bug is on your end, it’s all because you wrapped it in a pcall incorrectly. Your return is not going to the client but to the pcall.
Change the line where you use pcall to

local succ,result = pcall(function()

leave the return clone part as it is

and add

return result

after the pcall

I hope this will show you that no matter how sure you are, you should always test your code at every step to pinpoint where it goes wrong, I’d never find this error in your mess of a code if I didn’t write a simplified version of your script and test it. At which point I realized that the way you used pcall doesn’t actually return anything to the client.

Since the main code is in a pcall block, return clone returned the value to err and not the remotefunction.
At the end change to if not succ then warn(err) else return err end and see if it works

for _,x in w:GetChildren() do

Apparently this line was the cause. I thankfully had a spare autosave to go and check for any differences and I previously used GetDescendants() when it was working.

Funny enough, I now get the same error on another model, but that’s for another topic.

Please look into the pcall part because they way it’s set up now will not return the instance to the client.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.