Unable to cast value to Objects

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!

1 Like

What line is the error happening on? Also I’m pretty sure sure that FindPartsInRegion3 doesn’t return nil if no parts were in the table, you might be better off asking if #vehicle == 0 then rather than if vehicle == nil then.

The error references the remote function handler (local script)

[13:14:03.867 - Script ‘Players.TheEternalGreat.PlayerGui.Shop.MainFrame.SideFrame.CarShopFrame.1.Spawn.LocalScript’, Line 10]

LS:

local player = game.Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteFunction = ReplicatedStorage:WaitForChild("SpawnCar")

local car = tonumber(script.Parent.Parent.Name)

script.Parent.MouseButton1Up:Connect(function()
	local spawnCar = remoteFunction:InvokeServer(car)
	print("Gui pressed")
end)



Oh right, yeah remote functions are a bit annoying that way. Can you tell me which is the last print that passed, then?

The last print statement that showed in the output was

print("1 or more car in folder")

Your problem is here:

local vehicleSeat = v:FindFirstChild("VehicleSeat")
local vehicle = workspace:FindPartsInRegion3WithWhiteList(region, vehicleSeat)

The Developer Hub page for the workspace::FindPartsInRegion3WithWhiteList function states the second parameter must be an array of objects. If you only want to check if the VehicleSeat is occupying the Region3 space, simply do this:

local vehicleSeat = v:FindFirstChild("VehicleSeat")
local vehicle = workspace:FindPartsInRegion3WithWhiteList(region, {vehicleSeat})
3 Likes

Thank you very much for your help :smiley: