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 clone something from replicated storage to workspace? Please help

I've tried this:

local rs = game.ReplicatedStorage

rs.SpawnPlane.OnServerEvent:Connect(function(plane)
	local existingPlane = game.Workspace:FindFirstChild(plane.Name)
	if existingPlane then
		existingPlane:Destroy()
	end
	
	local clonedPlane = plane:Clone()
	clonedPlane.Parent = workspace
	
	print("Spawned: " .. plane.Name .. "!")
end)

but it gives me this error:

10:20:01.263 ServerScriptService.SpawnPlane:10: attempt to index nil with 'Parent' - Server - SpawnPlane:10

please help im confused

2 comments

0

First of all, Function1; Your variable [plane is nil]

0

No, its defined as the object in ReplicatedStorage; the function is called from a LocalScript in a text button(which spawns the plane); inside the localscript, plane is defined as the model in ReplicatedStorage, and then i send signal to the RemoteEvent called SpawnPlane; along with the arguements of (plane).

2 answers

✓ Accepted answer

Hello fellow Human, I heard you need help, Firstly let me explain your errors.

First of all; Your Varaible from the function which is "plane" isn't assigned into anything, Which means it's nil

Second; You can't clone the function variable; plane but you may know

And third; The Output may either error, or Print "nil".

And use plane as a variable in line 4.

Here is your fixed script:

local rs = game.ReplicatedStorage

rs.SpawnPlane.OnServerEvent:Connect(function()
    local plane --Put here plane variable because it's nil
    local existingPlane = game.Workspace:FindFirstChild(plane.Name)
    if existingPlane then
        existingPlane:Destroy()
    end

    local clonedPlane = plane:Clone()
    clonedPlane.Parent = workspace

    print("Spawned: " .. plane.Name .. "!")
end)

To mention, Edit line 4 for the plane variable because I don't know exactly where you store it.

I hope this helped you :D

9 comments

0

i store the planes in a folder called "Planes"; the plane is defined in a LocalScript that targets a RemoteEvent; thats why i put "rs.SpawnPlane.OnServerEvent:Connect(function(plane))".

0

wait wth; so i replaced the code inside the function with this: "print(plane.Name)" and its printing my UserName???

0

this means that "plane" is defined as a player somehow...???

0

Wot

0

Also i cant use your script because im trying to get multiple planes; there are multiple textbuttons that spawn a plane, using only one serverscript to clone them into workspace.

0

Thanks i found a way to get all of the planes and find the one that was sent from the button; your post is marked as solution!

0

Have a nice day, Sir!

0

month later, (i fixed this a month ago tho) and if you were wondering what the issue was, it was that i forgot that firing events pass the Player who fired the event as the first arguement.

0

month later, (i fixed this a month ago tho) and if you were wondering what the issue was, it was that i forgot that firing events pass the Player who fired the event as the first arguement.

btw this is the code inside the LocalScript that is firing the RemoteEvent (SpawnPlane).

local button = script.Parent
local plane = game.ReplicatedStorage.Planes:FindFirstChild(button.Name)

while wait() do
	if plane then
		game.ReplicatedStorage.Planes:FindFirstChild(button.Name)
		break
	elseif button.Name == "Template" then
		break
	else
		warn('error finding plane')
		return
	end
end

button.MouseButton1Click:Connect(function()		
	button.Text = "Spawning plane..."
	wait(0.5)
	button.Text = button.Name
	game.ReplicatedStorage.SpawnPlane:FireServer(plane)
end)

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