I am using a ray to detect where a bullet should go, and damage anything it touches
local Part,Position = workspace:FindPartOnRay(ray, {player.Character,script.Parent.Handle},false,true)
This is the code to detect anything it touches.
When this line is executed I get this error:
16:50:38.518 - Unable to cast value to Object
16:50:38.519 - Stack Begin
16:50:38.520 - Script 'Workspace.Pistol.WeaponScript', Line 3
16:50:38.521 - Stack End
Please explain why this is happening and how I can fix and prevent it in futer projects.
FindPartOnRay only accepts a single object/instance for its 2nd argument (not a table), but i would recommend using the new api (WorldRoot:Raycast) for raycasting since FindPartOnRay is deprecated now anyways.
This is the full script. (The event dosnt matter because I have already tracked the problem to this script)
script.Parent.Fire.OnServerEvent:Connect(function(player,from,to,range)
local ray = Ray.new(from, (to - from).unit*range)
local Part,Position = workspace:FindPartOnRay(ray, {player.Character,script.Parent.Handle},false,true)
if Part then
local hum = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
if hum then
hum:TakeDamage(script.Parent.Damage.Value)
end
end
end)