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

Cars Spawning from within Each Other, How to Put Distance in Between Them?

Asked by 6 years ago

I am trying to spawn cars based on the number of players in a racing game. However, it seems to be that depending on the number of players -- those cars spawn within each other, therefore, glitching. Because of this, you can't drive them either. The car model is in ServerStorage and being called from there. But since models do not have a .Position property they can't be separated with that property. What can I do in order to put distance/separate in between the cars, so they can be drive-able?

This is the code:

function createCars()
    for _, object in pairs(game.ServerStorage:GetChildren()) do
        local carCopy = game.ServerStorage.Car:Clone()
        carCopy.Parent = game.Workspace
        carCopy:MakeJoints()
    end
end

for i = 1, game.Players.NumPlayers do
    createCars()
end
0
When you put the Car in ServerStorage, the position of the where it was originally will have it be cloned there. Meaning lets say you had your Car in the center of the map. When you cut that Car or you place it from Workspace to ServerStorage, it's going to be cloned in the center. So all you have to do is place it in two different spots while the cars are in workspace, then place it in serverstor LukeGabrieI 73 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

USE THE MOVETO() FUNCTION

The first thing you should do is move the car into ServerStorage. Since models do not have a .Position property, you can use :MoveTo( vector3.new( x, y, z ) ) as used below, which places the cars in race car formation. It is up to you how you want the cars.

function createCars()
    for i = 1, numPlayers do
        for _, object in pairs(game.ServerStorage:GetChildren()) do
            local carCopy = game.ServerStorage.Car:Clone()
            carCopy.Parent = game.Workspace
            carCopy:MakeJoints()
            if i % 2 == 0 then
                --Sets the distance between cars for even players
                carCopy:MoveTo(Vector3.new(-184, 0, 95+i*15))
            else
                --Sets the distance between cars for odd players
                carCopy:MoveTo(Vector3.new(-208, 0, 95+i*15))
            end
        end
    end
end

createCars()

One small tip: place the create cars function inside the game loop and call the function from within the game loop. Doing so helps update the players when the game starts again.

0
Dude, I copied this from you, because I wanted people to give answers so I can give you an answeer@ Prime. Dxsigned 12 — 6y
0
Roblox Lua is different from JavaScript, a lot of time one has to figure the problem out, alone. To me that is what makes scripting fun. PrimeAlphinex 36 — 6y
Ad

Answer this question