Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to cast value to object?

When I use this script, I get this error:

15:07:18.573 - Unable to cast value to Objects 15:07:18.573 - Script 'Players.Player1.Backpack.Chem Sniper.LocalScript', Line 14 15:07:18.574 - Stack End

How do I fix it?

local tool = script.Parent
local user
local IgnoreList = {"Handle", "Hat"}


tool.Equipped:connect(function(mouse)
   
   user = tool.Parent 

   
   mouse.Button1Down:connect(function()
       
      local ray = Ray.new(tool.AimPart.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
      local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, user)

       
      local playerhit = game.Players:GetPlayerFromCharacter(hit.Parent) or game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
	  if playerhit then
	  local humanoid = playerhit.Character.Humanoid
	  print(hit.Name)
      if humanoid then
           if hit.Name == "Head" then
			humanoid:TakeDamage(100)
			else
			humanoid:TakeDamage(30)
			end
       end

      	-- Settings
       local distance = (position - tool.AimPart.CFrame.p).magnitude
       local rayPart = Instance.new("Part", user)
       rayPart.Name          = "RayPart"
       rayPart.BrickColor    = BrickColor.new("Really blue")
	   rayPart.Parent 		 = game.Workspace
       rayPart.Transparency  = 0.5
       rayPart.Anchored      = true
       rayPart.CanCollide    = false
       rayPart.TopSurface    = Enum.SurfaceType.Smooth
       rayPart.BottomSurface = Enum.SurfaceType.Smooth
       rayPart.formFactor    = Enum.FormFactor.Custom
       rayPart.Size          = Vector3.new(0.2, 0.2, distance)
       rayPart.CFrame        = CFrame.new(position, tool.AimPart.CFrame.p) * CFrame.new(0, 0, -distance/2)
   
       game.Debris:AddItem(rayPart, 0.1)
	   end
	end)
end)

1 answer

The second argument of FindPartOnRayWithIgnoreList has to be a table of parts. Replace user with a list of all of the parts inside of user.

edit: If you use FindPartOnRay instead, you don't have to change anything else, because the second argument can be an instance.

2 comments

0

Or, use FindPartOnRay instead

0

I tried doing that, but now it's not doing anyting. Also, @BlueTaslem, That was what I was originally going with, but it had issues with detecting shots to the head.

This discussion is preserved as it was. New answers, comments, and votes are closed.