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