Script not finding the parent, parent of handle

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    A script that takes damage with raycasting when you click. The script now is seeing if it hits a hat (handle) or anything else to take damage.
  2. What is the issue?
    Its not working. On hats. (Humanoid is not a valid member of Accessory “Workspace.Irlkalei.Hat” - Client - LocalScript:24)
local RunService = game:GetService("RunService")
local cam = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local item = nil

local function MouseRayCast()
	local mousepos = UIS:GetMouseLocation()
	local mouseray = cam:ViewportPointToRay(mousepos.X, mousepos.Y)
	local rcr = workspace:Raycast(mouseray.Origin, mouseray.Direction*1000)
	return rcr
end

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local result = MouseRayCast()
		print(result) -- what it hits
		if result and result.Instance then
			if result == ("Handle") then
					result.Instance.Parent.Parent.Humanoid.Health = result.Instance.Parent.Parent.Humanoid.Health - 25
				else
				result["Instance"].Parent.Humanoid.Health = result["Instance"].Parent.Humanoid.Health - 25
			end
		end
	end
end)

RunService.RenderStepped:Connect(function()
	local result = MouseRayCast()
	if result and result.Instance then
		item = result.Instance
	end
end)

  1. What solutions have you tried so far?
    Ai, redoing code.
1 Like

Hello! Seems like you’re trying to check if result == “Handle”, result isn’t a string so this check is always gonna fail. You can just do instead:

if result.Instance.Name == "Handle" then

Since the name of the instance is actually a string then the check would actually work now.

1 Like

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