How do I make a GUI that, when you press a specific button, it clones a model? (Car for my specific case.)
I am attempting at making this but with no good results, here is a little instruction set on how it works:
- GUI Opens Up with a list of available cars to select (Spawn)
- Next, once you click the car (Image Button) you would like to spawn, a new gui on the side will appear with a Select Button, the Car Stats (These are not really important) and the Car's Name.
- Once you click the Select button, it finds the selected car name and sets off a remoteEvent with the carName variable attached to it
- Once the remoteEvent starts (Called SpawnCar) a server sided script should get the carName and search for that carName in the cars folder in the serverStorage
- Then if the car is found it will clone it, change the name to plrName"sCar", and move it to the workspace
Here is the code that I made:
Spawn Car Script (Server Sided Script):
-- Spawn Car Event local event = game:GetService("ReplicatedStorage"):WaitForChild("CarEvent").SpawnCar -- Finally making the car work event.OnServerEvent:Connect(function(plr, carName) local car = game:GetService("ServerStorage"):WaitForChild("Cars"):FindFirstChild(carName) local cloneCar = car:Clone() cloneCar.Parent = workspace cloneCar.Name = plr.Name.."sCar" cloneCar:SetPrimaryPartCFrame(workspace.CarMovePart.CFrame) plr.Character.HumanoidRootPart.CFrame = workspace.CarMovePart.CFrame end)
GUI Button Script (Client Sided LocalScript):
-- Open Menu Frame (Exit Garage and Spawn Car) local currentFrame = script.Parent.Parent.Parent -- Current Frame local button = script.Parent local debounce = false local camera = workspace.Camera local garageCamera = workspace.Garage.GarageCamera local carCamera = workspace.Garage.CarCamera local focusPoint = workspace.Garage.CarSpawnPart3 local selectFrame = script.Parent.Parent.Parent.SelectFrame local rs = game:GetService("ReplicatedStorage") local carName = script.Parent.Parent.CarName.Value.Text -- Closing button.MouseButton1Click:Connect(function() local showCar = focusPoint:WaitForChild("ShowCar") if debounce == false then --Closing debounce = true currentFrame:TweenPosition(UDim2.new(0.26, 0,1.081, 0), "Out", "Quad", 0.5, false) -- Closes Current Frame camera:Interpolate(garageCamera.CFrame, focusPoint.CFrame, 1) selectFrame:TweenPosition(UDim2.new(-0.651, 0,-1.58, 0)) -- Closes Select Frame wait(0.5) selectFrame.Visible = false wait(0.5) currentFrame.Visible = false wait(0.5) rs.CarEvent.SpawnCar:FireServer(carName) -- Fires RemoteEvent wait(3) debounce = false end end)
So, each time I run it it gives off the error: ServerScriptService.SpawnCarScript:10: attempt to index nil with 'Clone'
Any help would be appreciated, thanks.
Also, I did it this way instead of hard coding the name of the car into the code to keep it more flexible and just be able to duplicate the script and make it work even with a different button, It is ran on Studio for testing (Play Button)
Screenshots of this:
Before Pressing Select Button:
After Pressing the Select Button (Look at Bottom):
you also need a instanceClone() script so you can clone it.
heres an example,
-- Get a reference to an existing object local original = workspace.Model -- Create the model copy local copy = original:Clone() -- Parent the copy to the same parent as the original copy.Parent = original.Parent -- Move the copy so it's not overlapping the original copy:SetPrimaryPartCFrame(CFrame.new(0, 50, 0))