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()
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.
wait
before placing the player in the seat? – Kylaaa Commented Jan 31 at 14:49