Help with raycast damaging player

local Debounce = false
local ChangeStateNumber = 0





local Sword_Module = {}



function Sword_Module.Slash(ToolName, PPC,plr)
	local char = workspace:WaitForChild(plr.Name)
	local Humanoid = char:WaitForChild("Humanoid")
	
	if not Debounce and ChangeStateNumber == 0 then
		Debounce = true
		ChangeStateNumber = 1
		
		local blacklist = {}
		
	
		
		for i,v in pairs(workspace.Mobs:GetDescendants()) do
			if v:IsA("Model")  then
				local direction = v.HumanoidRootPart.Position - char.HumanoidRootPart.Position
				local raycastresuly = workspace:Raycast(char.HumanoidRootPart.Position,direction)
				if raycastresuly.Instance then
					if raycastresuly.Distance < 5 and raycastresuly.Instance ~= workspace:WaitForChild(plr.Name) then
						raycastresuly.Instance.Parent.Humanoid:TakeDamage(10)
						print(raycastresuly)
					else
						print("To far!")
					end
					
				end
			end
		end
		script["sword slash"]:Play()
		Humanoid.Animator:LoadAnimation(script.Slash1):Play()
		script["Sword Swish 102 (SFX)"]:Play()
		wait(0.6)
		Debounce = false
		ChangeStateNumber = 0
	end
		
		
end

return Sword_Module

the code above is what i am doing i am trying to make the raycast only damage npcs but it damages the player. how could i fix this?

for i,v in pairs(workspace.Mobs:GetDescendants()) do
   if v:IsA("Model") and v.Name ~= char.Name then
      ––your code
   end
end

but how would v get the player since its looping through the mobs folder?

it still does damage what else could i do?

Can I see the script that requires this module?


local Module = require(game.ReplicatedStorage.GameHandler.Sword_Module)
local plr = script.Parent.Parent.Parent


script.Parent.Activated:Connect(function()
	task.delay(0.1, function()
		Module.Slash(script.Parent.Name, script.Parent.PPC, plr)

	end)
end)

this is a server script inside the tool this is where it rquires it from

I have fixed the problem thanks for your help though!

1 Like