lua - How to spawn a player in a car? - Stack Overflow

admin2025-04-18  4

I wrote a code that when a player spawns in Roblox, immediately spawns him behind the wheel of my car model (that is, you do not need to press any extra keys). I took the car model from the Toolbox and placed it in Workspace (model ID 111087632034479). The car model itself has all the functionality for its operation.

But I can not understand why when I try to move the car model from Workspace to ServerStorage or ReplicatedStorage, my script for automatically getting the player into the car stops working?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local LOBBY_START_TIME = 5
local MIN_PLAYERS = 1

local carsFolder = Workspace:WaitForChild("Cars")
local lobbySpawn = Workspace:WaitForChild("LobbySpawn")

local function assignCarToPlayer(player)

    for _, car in pairs(carsFolder:GetChildren()) do
        if car:FindFirstChild("DriveSeat") and not car.DriveSeat.Occupant then

            local character = player.Character or player.CharacterAdded:Wait()
            if character and character:FindFirstChild("Humanoid") then
                local humanoid = character:FindFirstChild("Humanoid")


                character:SetPrimaryPartCFrame(car.DriveSeat.CFrame) 


                car.DriveSeat:Sit(humanoid)

                print(player.Name .. " seating in car!")
                break
            end
        end
    end
end


local function startGame()
    print("Game is started!")
    for _, player in pairs(Players:GetPlayers()) do
        assignCarToPlayer(player)
    end
end

local function waitForPlayers()
    while true do
        if #Players:GetPlayers() >= MIN_PLAYERS then
            print("Enought players " .. LOBBY_START_TIME .. " seconds.")
            wait(LOBBY_START_TIME)
            startGame()
            break
        else
            print("Waiting Players... " .. #Players:GetPlayers() .. "/" .. MIN_PLAYERS)
        end
        wait(1)
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:SetPrimaryPartCFrame(lobbySpawn.CFrame)
    end)
end)

waitForPlayers()

I wrote a code that when a player spawns in Roblox, immediately spawns him behind the wheel of my car model (that is, you do not need to press any extra keys). I took the car model from the Toolbox and placed it in Workspace (model ID 111087632034479). The car model itself has all the functionality for its operation.

But I can not understand why when I try to move the car model from Workspace to ServerStorage or ReplicatedStorage, my script for automatically getting the player into the car stops working?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local LOBBY_START_TIME = 5
local MIN_PLAYERS = 1

local carsFolder = Workspace:WaitForChild("Cars")
local lobbySpawn = Workspace:WaitForChild("LobbySpawn")

local function assignCarToPlayer(player)

    for _, car in pairs(carsFolder:GetChildren()) do
        if car:FindFirstChild("DriveSeat") and not car.DriveSeat.Occupant then

            local character = player.Character or player.CharacterAdded:Wait()
            if character and character:FindFirstChild("Humanoid") then
                local humanoid = character:FindFirstChild("Humanoid")


                character:SetPrimaryPartCFrame(car.DriveSeat.CFrame) 


                car.DriveSeat:Sit(humanoid)

                print(player.Name .. " seating in car!")
                break
            end
        end
    end
end


local function startGame()
    print("Game is started!")
    for _, player in pairs(Players:GetPlayers()) do
        assignCarToPlayer(player)
    end
end

local function waitForPlayers()
    while true do
        if #Players:GetPlayers() >= MIN_PLAYERS then
            print("Enought players " .. LOBBY_START_TIME .. " seconds.")
            wait(LOBBY_START_TIME)
            startGame()
            break
        else
            print("Waiting Players... " .. #Players:GetPlayers() .. "/" .. MIN_PLAYERS)
        end
        wait(1)
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:SetPrimaryPartCFrame(lobbySpawn.CFrame)
    end)
end)

waitForPlayers()
Share Improve this question edited Mar 31 at 18:11 Anerdw 2,1023 gold badges16 silver badges40 bronze badges asked Jan 29 at 4:12 user29410560user29410560 1 2
  • Could you share the code that doesn't work? You've said that the code works when the cars are in the Workspace, but not in ReplicatedStorage. When you clone the car from the ReplicatedStorage, do you wait before placing the player in the seat? – Kylaaa Commented Jan 31 at 14:49
  • @Kylaaa the thing is that if the car model is in replicatedstorage or in serverstorage - the script puts the character on driveseat not as it should - that is, a speedometer strip appears at the bottom and the inscription "speed", although in my car model everything is already installed and registered, all the plugins and all the scripts - that is, there is its own speed panel, etc. You can check it yourself on a realistic car model from the toolbox from a-chassis – user29410560 Commented Feb 5 at 2:57
Add a comment  | 

1 Answer 1

Reset to default 1

If you are getting an infinite yield warning, it is Because carsFolder refers to the Workspace variable, which is game:GetService('Workspace'). Change it to ReplicatedStorage or ServerStorage depending on what you are using.

After this, make sure you are changing the car parent to the workspace so that it spawns in correctly.

转载请注明原文地址:http://anycun.com/QandA/1744986252a90330.html