I need to know why this isn’t working correctly. I’m trying to use raycasting to determine how far away the humanoid is (In this case I have the NPC humanoids named “Monster”) and deal damage depending on if they are close enough.
local function onTouched(hit)
local character = hit.Parent
local targetHumanoid = character:FindFirstChild("Monster")
if targetHumanoid and isActivated then
-- Use raycasting to check the distance
local ray = Ray.new(sword.Handle.Position, (hit.Position - sword.Handle.Position).unit * 5)
local part, position = workspace:FindPartOnRay(ray, character, false, true)
if part and part.Parent == character and part.Parent:IsA("Model") and part.Parent.Name == "Monster" then
-- Deal damage to the humanoid (client-side only)
targetHumanoid:TakeDamage(10) -- Damage the Monster will take (Differs by the sword)
end
end
end
What are you checking for hits? The handle of the tool?, i’d also check if local targetHumanoid = character:FindFirstChild("Monster") this is actually found by printing it first. It has most likely something to do with the if statements at the end tbh