Objects are nil when I fire an event

Hello everyone,

I am writing an placement script and I have encountered a problem: objects are outputting as nil when passed through a remote event. The script worked when the placement script only had to deal with one object.

Here is the code:
Server Script:


--Server Script:
local function place(player, part, pos)
	print(part) -- says nil
	local p = game.ReplicatedStorage.Objects:FindFirstChild(part):Clone()
	p.Parent = folder 
	p:SetPrimaryPartCFrame(pos)
end

re.OnServerEvent:Connect(place)

Local Script:

--Local Script:
	if canPlace == true then
		AnimateClick(part)
		re = game.ReplicatedStorage:FindFirstChild("ServerPlacement")
		local model = part
		re:FireServer(model, model.PrimaryPart.CFrame)
		print(model) --this work, it outputs as part (which is what I want)
	end

If I need to add more info, please ask;

Thanks for the help!

That is because the part has been created on the client, and did not replicate on the server. You should create the part in the Server Script.

What do you mean, I did clone it, it is just saying nil

Yeah, but you are cloning it on the client. When you create or modify instances on the Client, it does not replicate to the Server because of a safeguard named FilteringEnabled. You can read more here.

(blokav gave a better example)

If part was instantiated or cloned on the client script, then it can’t exist on the server. Instead the client should send a signal to the server telling it which part to create/clone, (with security checks of course).

1 Like

Can you provide me an example or a link, cause I am confused.

Please retry your answers, since I have edited the post

So where are you creating the part?

local part = game.Workspace.Part:Clone()  -- local script

If this is from a local script the server won’t be able to access the part.

You need to create the part in the server,

The reason your part is returning nil is because it didn’t exist in the server at the time it was printed. In just the next line after, it was cloned into the server. If you switched the positions of those two lines, it would have returned the part.

fixed code:

local function place(player,part,pos)
	local p = part:Clone()
	p:SetPrimaryPartCFrame(pos)
	p.Parent = folder
	print(p.Name)
end

I know but when I try to clone the part I get Argument 1 missing or nil, which I why I printed the statement to check if it exists; the print statement is not the problem, the next line (where I clone the part) is the problem.

Exactly. You are trying to clone something that doesn’t exist.

If you want a part on the server, create it in the server instead. If you want the part to simply appear in the server once it’s interacted with, save the CFrame of the part, delete it, and instance it in the server in your OnServerEvent function. If the part is a model, you can save the model and use InsertService:LoadAsset().

Then is my local script incorrect, because the code is from other projects - which were successful - but only handled one object. I used an remote event and sent information to a server script to create the part with the respective CFrame.

Thanks everyone for responding, my friend solved the problem: I was sending wrong information to the server and redefining every variable.