hello i am making a driving game and i am making a spawn car script from a gui.
repeat wait() until game.Players.LocalPlayer.Character local player = game.Players.LocalPlayer local char = player.Character local rp = game.ReplicatedStorage local v1 = rp["Vehicles "].Vehicle1 local v2 = rp["Vehicles "].Vehicle2 script.Parent.Vehicle1.MouseButton1Click:Connect(function() local ClonedVehicle = v1:Clone() wait() ClonedVehicle.Parent = workspace wait() ClonedVehicle:MakeJoints() wait() ClonedVehicle.PrimaryPart = ClonedVehicle.Main wait() ClonedVehicle:SetPrimaryPartCFrame(char.Head.CFrame) end)
if anyone knows how to make this script work in game it would help a lot thanks
the car in game spawns but just floats in the air and wont work
Vehicle
is already floating in the Workspace
What do I mean by that? When you put a Model
or a Part
in Workspace,
it automatically puts it at the center of the Workspace.
The thing about the Clone()
feature is that it will clone to that specific part if the script hasn't actually told the model to move somewhere.
Example.
Car Model
at 0, 0, 0
. Workspace
. Model
is up. It is always a good Idea to check whether this is your case. If not then let's move on.
Anchored
your Vehicle
in the Workspace
.Anchoring
is a big no-no for a Vehicle
. A Vehicle cannot move if it is Anchored
and will simply stay on one position if so. Which is why this is a follow through of my first point.
Check if your Model is at the right Position that you want and is not Anchored.
Vehicle
by simply utilizing MoveTo()
.MoveTo()
is a feature designed for Models
to move the Model freely around the Workspace
.
MoveTo()
takes in Vector3
values and just positions like any other parts.
local Vector3New = Vector3.new; NewCar:MoveTo(Vector3New(100, 0, 0));
Here is my working code. Feel free to change it up to your desire.
-- DeclarationS Section -- //Game Services local ReplicatedStorage = game:GetService("ReplicatedStorage"); local Workspace = game:GetService("Workspace"); local Player = game:GetService("Players").LocalPlayer; local Character = Player.Character or Player.CharacterAdded:Wait(); local PlayerGui = Player:WaitForChild("PlayerGui"); -- //Car Location local Car = ReplicatedStorage:WaitForChild("Car"); -- //Gui Location local CaSpawningGui = PlayerGui:WaitForChild("CarSpawningGui"); local Button = CaSpawningGui:WaitForChild("Button"); -- //Character Location local Head = Character["Head"]; -- //Variables local NewCar; -- //Game Variables local Vector3New = Vector3.new; -- Processing Section local function SpawnGuiWhenClicked () print("Envoked function"); NewCar = Car:Clone(); NewCar.Parent = Workspace; NewCar:MoveTo(Vector3New(10, 0, 0)); NewCar:MakeJoints(); NewCar.PrimaryPart = NewCar.DriveSeat; NewCar:SetPrimaryPartCFrame(Head.CFrame); if Workspace:FindFirstChild("Car") then print("Found") end; end; -- Connecting Section Button.MouseButton1Down:Connect(SpawnGuiWhenClicked);