Unable To Cast Array To Vector3?

Hi, Im trying to add a feature in my game where you can build your own structures, I figured out that

FindPartOnRayWithIgnoreList() 

is depreciated so I used RaycastParams() and Raycast(), but I’m getting the error “Unable to cast Array to Vector3”

Here’s my script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local MaterialFolder = ReplicatedStorage:WaitForChild("Materials")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local Character = player.Character or player.Character:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local Mouse = player:GetMouse()

local yAxisBuildingOffset = 5
local MaxPlaceDistance = 50
local RotateKeyIsPressed = false
local PlacingStructureDebounce = false

for _, structureButton in pairs(StructureFrame:GetChildren()) do
	if structureButton:IsA("TextButton") then
		structureButton.MouseButton1Down:Connect(function()
			
			StructureFrame.Visible = false
			
			local yOrientation = 0
			local InRangeToPlace = false
			local placedStructure
			
			if PlacingStructureDebounce == false then
				PlacingStructureDebounce = true
				
				local ClientStructure = MaterialFolder:FindFirstChild(structureButton.Name):Clone()
				ClientStructure.BrickColor = BrickColor.new("Forest green")
				ClientStructure.Material = "Neon"
				ClientStructure.CanCollide = false
				ClientStructure.Parent = game.Workspace
				
				local StartingCFrame = CFrame.new(0, -2, -15)
				ClientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingCFrame)
				
				RunService.RenderStepped:Connect(function()
					local MouseRay = Mouse.UnitRay
					local CastRay = RaycastParams.new(MouseRay.Origin, MouseRay.Direction * 1000)
					local IgnoreList = {ClientStructure, Character}
					local hit, Position = workspace:Raycast(CastRay, IgnoreList)
					
					if hit and (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidRootPart.Position - ClientStructure.Position).Magnitude < MaxPlaceDistance then
						InRangeToPlace = true
						ClientStructure.BrickColor = BrickColor.new("Forest green")
					else
						InRangeToPlace = false
						ClientStructure.BrickColor = BrickColor.new("Crimson")	
					end
					
					local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
					local NewCFrame = CFrame.new(Position.X, Position.Y + yAxisBuildingOffset, Position)
					ClientStructure.CFrame = NewCFrame * NewCFrame
				end)
				
				UIS.InputBegan:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						RotateKeyIsPressed = true
						
						local RotationSpeed = 3
						
						while RotateKeyIsPressed do
							wait()
							if PlacingStructureDebounce == true then
								yOrientation = yOrientation + RotationSpeed
							end
						end
					end
				end)
				
				UIS.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						RotateKeyIsPressed = false
					end
				end)
				
				UIS.InputBegan:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						if PlacingStructureDebounce == true then
							if InRangeToPlace == true then
								local StructureCFrame = ClientStructure.CFrame
								placedStructure = PlaceStructure:InvokeServer(ClientStructure.Name, StructureCFrame)
								
								if placedStructure == true then
									PlacingStructureDebounce = false
									ClientStructure:Destroy()
									StructureFrame.Visible = true
								end
							end
						end
					end
				end)
			end
		end)
	end
end

I haven’t worked with these too much, so it became a little harder to figure out.

1 Like

The 2nd paramater of game:Raycast is the direction, the 3rd is raycastParams.
Additionally, RaycastParams.new doesn’t take any arguments.

1 Like