How to make player take damage when touch ray?

FindPartOnRayWithIgnoreList is deprecated, try not to use that (Use the RayCast result & RaycastParams instead)

Try this script?

–Create a part above player head
local hitbox = game.ReplicatedStorage.KiroDF.Hitbox:Clone()
hitbox.Parent = plr.Character.UpperTorso
hitbox.CFrame = wind1.CFrame * CFrame.new(0,30,0)

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {hitbox}

local Direction = hitbox.CFrame.LookVector * 50

–Cast a ray from that part
while wait() do
    if plr.Character.UpperTorso:FindFirstChild("Hitbox") then
        local RayResult = workspace:RayCast(hitbox.Position, Direction, raycastParams)
    
        if RayResult then
            print("Part result hit: "..RayResult.Instance)
            local HitPart = RayResult.Instance
            local Model = HitPart.Parent
            if Model:FindFirstChild("Humanoid") and Model.Humanoid.Health > 0 then
                Model.Humanoid:TakeDamage(25)
                print("Damage activated")
            end
        else
            print("No result found")
        end
    else
        break
    end
end

I think you’re trying to fire rays from the entire surface on the part rather than from a point. To do this, you’ll have to use part.Size.X and part.Size.Z to randomly determine a random position below the part’s surface. Note that parts which are slightly angled may have to use a little more complicated method.

Random position on BottomSurface

local randomPosition = hitbox.Position * Vector3.new(math.random(-hitbox.Size.X / 2, hitbox.Size.X / 2), -hitbox.Size.Y, math.random(-hitbox.Size.Z / 2, hitbox.Size.Z / 2))
1 Like

error : RayCast is not a valid member of Workspace"Workspace"
at this line : local RayResult = workspace:RayCast(hitbox.Position, Direction, raycastParams)

Change RayCast to Raycast as it’s case sensitive

1 Like

it’s not detecting humanoid, is there any way to visualize ray?

Is the part above you? I think direction has to be

local Direction = hitbox.CFrame.UpVector * 50

So it raycasts upward, And if you want to visualise a ray, just refer to this

1 Like

The only problem now is the ray keep detecting accesories (my english broken(or broke?))

I think you hae to filter yourself as well

change

raycastParams.FilterDescendantsInstances = {hitbox}

To

raycastParams.FilterDescendantsInstances = {plr.Character}

So it wont detect you as well, least that’s what I think is needed

Please correct me if you don’t want yourself to be excluded from the Raycasy

1 Like

I want myself to be excluded from the raycast but now it’s not detect any humanoid

     while wait() do
            			if plr.Character.UpperTorso:FindFirstChild("Hitbox") then
            				local RayResult = workspace:Raycast(hitbox.Position, Direction, raycastParams)

            				if RayResult then
            					print(RayResult.Instance.Parent.Name)
  --when ray cast it will print "Workspace"
                                                print(RayResult.Instance.Parent.Name)
  --when ray cast it will print "Part"
            					if RayResult.Instance.Parent:FindFirstChild("Humanoid") and RayResult.Instance.Parent.Humanoid.Health > 0 then
            						RayResult.Instance.Parent.Humanoid:TakeDamage(25)
            						print("Damage activated")
            					end
            				else
            					print("No result found")
            				end
            			else
            				break
            			end
            			end

I think you need to visualize the Raycast so we can see what’s happening exactly

1 Like

ray damage work fine when i remove my accesories so I just need to filter all of the accessories in this line

raycastParams.FilterDescendantsInstances = {hitbox}

but idk how to do that

Maybe try

raycastParams.FilterDescendantInstances = {hitbox, plr.Character.Humanoid:GetAccessories()}
1 Like

It stop detecting my accessories but how to do the same thing with npc/other player accessories?

error : attempt to index nil with ‘GetAccessories’ (there are end for ‘for i, v…’ above these lines)

        for i ,v in pairs(game.Workspace:GetChildren()) do
        			local huma = v:FindFirstChild("Humanoid")					
        		
        		local raycastParams = RaycastParams.new()
        		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
        			raycastParams.FilterDescendantsInstances = {plr.Character, hitbox, huma:GetAccessories()}

not detect anything

  local randomPosition = hitbox.Position * Vector3.new(math.random(-hitbox.Size.X / 2, hitbox.Size.X / 2), -hitbox.Size.Y, math.random(-hitbox.Size.Z / 2, hitbox.Size.Z / 2))
    local RayResult = workspace:Raycast(randomPosition, Vector3.new(0,-200,0), raycastParams)

The second argument is not correct, apparently? You’re supposed to use the original position multiplied by a vector that is 200 studs below it.

thx for helping me the only problem left is I can’t find npc’s humanoid in ray.

I tested

print(RayResult.Instance.Parent.Name)
--and
print(RayResult.Instance.Name)				

result : https://gyazo.com/7a8138b4fc891b1b0ac26d2e98a93475

entire raycasting script

    local hitbox = game.ReplicatedStorage.KiroDF.Hitbox:Clone()
    		hitbox.Parent = plr.Character.UpperTorso
    		hitbox.CFrame = wind1.CFrame * CFrame.new(0,30,0)
    		
    		for i ,v in pairs(game.Workspace:GetChildren()) do
    			local huma = v:FindFirstChild("Humanoid")
    			if huma then
    				local ac = v.Humanoid:GetAccessories()
    			
    		local raycastParams = RaycastParams.new()
    		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist			
    		raycastParams.FilterDescendantsInstances = {hitbox, ac, plr.Character}
    		while wait() do
    			if plr.Character.UpperTorso:FindFirstChild("Hitbox") then
    				local VCharacter = game.Workspace:FindFirstChild(plr.Name)
    				local vPlayer = game.Players:GetPlayerFromCharacter(VCharacter)
    				local randomPosition = hitbox.Position + Vector3.new(math.random(-hitbox.Size.X / 2, hitbox.Size.X / 2), -hitbox.Size.Y, math.random(-hitbox.Size.Z / 2, hitbox.Size.Z / 2))
    				local RayResult = workspace:Raycast(randomPosition, hitbox.Position * Vector3.new(0,-200,0), raycastParams)			
    				
    				if RayResult then						
    					print("test1 : "..RayResult.Instance.Parent.Name)
    					print("test2 : "..RayResult.Instance.Name)							
    					if RayResult.Instance.Parent.Parent:FindFirstChild("Humanoid") and not RayResult.Instance.Parent.Parent:FindFirstChild("CheckSmash")then						
    						local check = Instance.new("BoolValue")
    						check.Name = "CheckSmash"
    						check.Parent = RayResult.Instance.Parent.Parent
    						game.Debris:AddItem(check, 0.5)
    						tagEnemy(RayResult.Instance.Parent.Parent:FindFirstChild("Humanoid"),vPlayer)
    						RayResult.Instance.Parent.Parent.Humanoid:TakeDamage(25)	
    						wait(0.05)
    						untagEnemy(RayResult.Instance.Parent.Parent:FindFirstChild("Humanoid"))	
    						print("damagedone")
    					end				
    				end
    			else
    					break
    					end
    				end
    			end
    		end

Questionable about its efficiency when I already saw this line. Be careful with that.


On another note, try creating a random part with position as the randomPosition that lasts long enough to be seen and analyzed.

1 Like

I use this line to detect all accessories in workspace do i have to fix something?

Instead, if you hit an accessory, you know the ancestor of the part is at least a model to find the character. Using FindFirstAncestorOfClass would totally help.

1 Like