Unable to cast RaycastParams to Vector3

Hi so I am trying to make a trident go to players in a list but Im having a problem

error

Unable to cast RaycastParams to Vector3
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr, list)
	local tobject = game.ReplicatedStorage.trident:Clone()
	tobject.Parent = workspace

	for i, player in pairs(list) do
		local playerRootPart = game.Workspace:FindFirstChild(player)
		print(player)
		if playerRootPart then
			local bodyPosition = Instance.new("BodyPosition")
			bodyPosition.Parent = tobject
			bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bodyPosition.Position = playerRootPart.HumanoidRootPart.Position

			tobject.Anchored = false

			local raycastParams = RaycastParams.new()
			raycastParams.IgnoreWater = true
			raycastParams.FilterDescendantsInstances = {tobject}

			while true do
				local direction = (playerRootPart.HumanoidRootPart.Position - tobject.Position).unit

				local ray = Ray.new(tobject.Position, direction * 100)
				local hitPart, hitPosition, hitNormal = workspace:Raycast(ray, raycastParams)

				if hitPart then
					local newPosition = hitPosition - (direction * (tobject.Size.magnitude / 2))
					local tweenInfo = TweenInfo.new((newPosition - tobject.Position).magnitude / 50, Enum.EasingStyle.Linear)
					local tween = game:GetService("TweenService"):Create(tobject, tweenInfo, {Position = newPosition})
					tween:Play()
				end

				wait(.1)
			end
		else
			warn("Could not find player or object")
		end
	end
end)

1 Like

Replace this:

with this:

local raycastResult = workspace:Raycast(tobject.Position, direction * 100, raycastParams)
local hitPart, hitPosition, hitNormal = raycastResult.Instance, raycastResult.Position, raycastResult.Normal

I get this error Players.Player1.Backpack.Fire.Script:24: attempt to index nil with ā€˜Instance’

1 Like

Oh sorry I think that’s because the Raycast didn’t hit anything so the raycastResult is nil. I think you can replace the ā€œif hitPart thenā€ with ā€œif raycastResult thenā€ and also move the line that sets the hitPart, hitPosition, and hitNormal variables under that conditional statement.

1 Like

but thats what allows the trident to tween

1 Like

Just do this:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr, list)
	local tobject = game.ReplicatedStorage.trident:Clone()
	tobject.Parent = workspace

	for i, player in pairs(list) do
		local playerRootPart = game.Workspace:FindFirstChild(player)
		print(player)
		if playerRootPart then
			local bodyPosition = Instance.new("BodyPosition")
			bodyPosition.Parent = tobject
			bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bodyPosition.Position = playerRootPart.HumanoidRootPart.Position

			tobject.Anchored = false

			local raycastParams = RaycastParams.new()
			raycastParams.IgnoreWater = true
			raycastParams.FilterDescendantsInstances = {tobject}

			while true do
				local direction = (playerRootPart.HumanoidRootPart.Position - tobject.Position).unit

				local raycastResult = workspace:Raycast(tobject.Position, direction * 100, raycastParams)

				if raycastResult then
					local hitPart, hitPosition, hitNormal = raycastResult.Instance, raycastResult.Position, raycastResult.Normal
					local newPosition = hitPosition - (direction * (tobject.Size.magnitude / 2))
					local tweenInfo = TweenInfo.new((newPosition - tobject.Position).magnitude / 50, Enum.EasingStyle.Linear)
					local tween = game:GetService("TweenService"):Create(tobject, tweenInfo, {Position = newPosition})
					tween:Play()
				end

				wait(.1)
			end
		else
			warn("Could not find player or object")
		end
	end
end)
1 Like

Thanks you so much this works.

1 Like

You mind marking mine as the solution for other people’s reference?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.