You can write your topic however you want, but you need to answer these questions:
-
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. -
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)
-
What solutions have you tried so far?
Ai, redoing code.