"Unable to cast value to Objects" error while raycasting

Code:

script.RemoteEvent.OnServerEvent:Connect(function(Player, Mouse)
		
	print("remote event fired")
    local Character = Player.Character
	
	local rp = RaycastParams.new()
	
	rp.FilterDescendantsInstances = (Character)
	
	rp.FilterType = Enum.RaycastFilterType.Blacklist
	
	local rr = workspace:Raycast(Character.HumanoidRootPart.Position, (Mouse - Character.HumanoidRootPart.Position)*300, rp)
	
	if rr then
		local hit = rr.Instance
		local hrp = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("HumanoidRootPart")
		if hrp then
			local fr = Instance.new("BodyVelocity", hrp)
			fr.MaxForce = Vector3.new(10000000,10000000,10000000)
			fr.Velocity = (hrp.CFrame.LookVector * -50) + (hrp.CFrame.UpVector * 50)
		end
	end
	
end)
1 Like

I don’t see anything that stands out as to being the problem. What’s on the client side?

I’ve ran into something like this recently. It’s caused by an API function that’s looking for an object but it’s getting a value instead.

1 Like

Here’s the client:

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local debounce = false

UIS.InputBegan:Connect(function(key, processed)
	if processed or debounce then 
		return 
	end

	if key.KeyCode == Enum.KeyCode.Q and game.Players.LocalPlayer.Class.Value == "Air" then
		debounce = true
		workspace.AirFlingScript.RemoteEvent:FireServer(mouse.Hit.Position)
	end

	wait(1)
	debounce = false
end)
1 Like

What line does it say the error is on? Screenshot of the error?

EDIT:

The only thing that I see that kind of stands out, but not related to the problem you are having, is this line:

local hrp = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("HumanoidRootPart")

This big issue with this is if the part that’s returned from Raycast doesn’t belong to a player or a model, then it returns nil. With FindFirstChild, that will throw an error. Something like this works better and will catch if FindFirstAncestorOfClass returns nil.

local arp = hit:FindFirstAncestorOfClass("Model")
if arp then
	hrp = arp:FindFirstChild("Humanoid")
	if hrp then
		-- Rest of your code here
	end
end

Other than that, I don’t see anything wrong, which leads me to believe that it’s a data issue: A variable contains one thing, but it should be something else.

1 Like

It actually shows that the error is here:

rp.FilterDescendantsInstances = (Character)
1 Like

Change it from () to {}

rp.FilterDescendantsInstances = {Character}

3 Likes

Try changing the parenthesis to curly braces. I think it’s trying to convert the Character object into a value…probably the name of the character. That parameter requires a table.

2 Likes

There is no coercion taking place, simply put, the ‘FilterDescendantsInstances’ property of ‘RaycastParams’ objects expects to represent a table of objects (instances), not a single object (instance).

1 Like