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
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.
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).
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.
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.