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)

3 comments

0

Can you post the output error?

0

Posted.

0

I haven't used FindPartOnRay before, but something you could try is replacing user on line 14 with a table of all the character's parts.