Hi there, I’m currently trying to make a car spawning system that when a player spawns a car, it will check if the spawn locations are currently occupied by another car.
The issue is that there is a car in one of the spawns, and I try spawning another car, it gives this error:
(Unable to cast value to Objects)
I have tried various time to identify the error to no avail. Other DevForum articles didn’t help.
I have several print statements within my code to identify the around where it breaks.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage:WaitForChild("SpawnCar")
local function spawnCar(player,car)
print("function started")
local spawnLocations = game.Workspace:WaitForChild("CarSpawns"):GetChildren()
local childrenInVehiclesFolder = game.Workspace:WaitForChild("VehiclesFolder"):GetChildren()
for i, v in pairs(spawnLocations) do
wait()
print("hi")
local object = v
local corner1 = object.Position - Vector3.new(object.Size.X/2, object.Size.Y/2, object.Size.Z/2)
local corner2 = object.Position + Vector3.new(object.Size.X/2, object.Size.Y/2, object.Size.Z/2)
print(corner1)
print(corner2)
local region = Region3.new(corner1, corner2)
local currentSpawnArea = v.Position
print("Position calculated")
if #childrenInVehiclesFolder ~= 0 then
print("1 or more car in folder") -- output will print this, but nothing after it
for i, v in pairs(childrenInVehiclesFolder) do
wait()
local vehicleSeat = v:FindFirstChild("VehicleSeat")
local vehicle = workspace:FindPartsInRegion3WithWhiteList(region, vehicleSeat)
print("loop running")
if vehicle == nil then
print("no seat found")
local clonedCar = ReplicatedStorage:WaitForChild("Cars"):FindFirstChild(car):Clone()
clonedCar.Parent = workspace:WaitForChild("VehiclesFolder")
clonedCar:MoveTo(currentSpawnArea)
local ownerVal = Instance.new("StringValue")
ownerVal.Value = player.Name
ownerVal.Name = "playerName"
ownerVal.Parent = clonedCar
print("Car Spawned!")
player.Character:WaitForChild("HumanoidRootPart").CFrame = clonedCar.PrimaryPart.CFrame + Vector3.new(5,0,5)
else
print("seat found")
break
end
end
else
print("nothing in folder")
local clonedCar = ReplicatedStorage:WaitForChild("Cars"):FindFirstChild(car):Clone()
clonedCar.Parent = workspace:WaitForChild("VehiclesFolder")
local ownerVal = Instance.new("StringValue")
ownerVal.Value = player.Name
ownerVal.Name = "playerName"
ownerVal.Parent = clonedCar
clonedCar:MoveTo(currentSpawnArea)
print("Car wasn't found")
player.Character:WaitForChild("HumanoidRootPart").CFrame = clonedCar.PrimaryPart.CFrame + Vector3.new(5,0,5)
break
end
end
end
remoteFunction.OnServerInvoke = spawnCar
Any help is appreciated, thanks!