I've tried this:
01 | local rs = game.ReplicatedStorage |
02 |
03 | rs.SpawnPlane.OnServerEvent:Connect( function (plane) |
04 | local existingPlane = game.Workspace:FindFirstChild(plane.Name) |
05 | if existingPlane then |
06 | existingPlane:Destroy() |
07 | end |
08 |
09 | local clonedPlane = plane:Clone() |
10 | clonedPlane.Parent = workspace |
11 |
12 | print ( "Spawned: " .. plane.Name .. "!" ) |
13 | 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:
01 | local rs = game.ReplicatedStorage |
02 |
03 | rs.SpawnPlane.OnServerEvent:Connect( function () |
04 | local plane --Put here plane variable because it's nil |
05 | local existingPlane = game.Workspace:FindFirstChild(plane.Name) |
06 | if existingPlane then |
07 | existingPlane:Destroy() |
08 | end |
09 |
10 | local clonedPlane = plane:Clone() |
11 | clonedPlane.Parent = workspace |
12 |
13 | print ( "Spawned: " .. plane.Name .. "!" ) |
14 | 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).
01 | local button = script.Parent |
02 | local plane = game.ReplicatedStorage.Planes:FindFirstChild(button.Name) |
03 |
04 | while wait() do |
05 | if plane then |
06 | game.ReplicatedStorage.Planes:FindFirstChild(button.Name) |
07 | break |
08 | elseif button.Name = = "Template" then |
09 | break |
10 | else |
11 | warn( 'error finding plane' ) |
12 | return |
13 | end |
14 | end |
15 |