Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my car float in the air in game when i spawn it?

Asked by 6 years ago

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

0
It could possibly be due to the fact that the car is anchored? gmatchOnRoblox 103 — 6y
0
I read your comment. Yes I know that it is in Replicated Storage. But you first want to put it in Workspace, position your model properly THEN put it in Replicated Storage. Zafirua 1348 — 6y
0
gmatch i tried it anchored and unanchored ad they both do the exact same TheRIPPER71 -2 — 6y
0
If you actually read my whole code, then you would see that I wrote use `MoveTo()` if it still errors. Either give me a youtube video or a uncopylocked version. Zafirua 1348 — 6y

1 answer

Log in to vote
0
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
6 years ago
Edited 6 years ago

There are Two Main reasons as to why this would happen.

  • The first reason being that your 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.

  • Say you have a Car Model at 0, 0, 0.
  • You then moved it up thinking that if it is down, it will go through the Workspace.
  • You run your script and the Model is up.

It is always a good Idea to check whether this is your case. If not then let's move on.

  • The second reason being that you have 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.

  • If error still persists, then you could manually set the location of the 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));
  • If that is not your case too then you would want to check the output. There could be an error which could be stopping your code.

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

Should you have any questions, feel free to comment below. Have fun debugging.

0
okay, the vehicle is not even in workspace, you would know that if you actually looked at the script. also, the script works completely fine in the studio, but does not work in game. as i said, it just floats there and does nothing. TheRIPPER71 -2 — 6y
Ad

Answer this question