How to make Script compatible with npcs?

Currently, it only dmgs players, but when I use don npcs it returns an error, I understand that it’s due to the leaderstats,but how would I make a work around it?

local module = {}

module.Hitbox = function(Character:Model, CFrameOffset:CFrame, Size:number, Damage:number, BlockDamage:number, VfxPart:BasePart)
	local Hrp = Character:FindFirstChildOfClass("Humanoid").RootPart
	local player = game.Players:GetPlayerFromCharacter(Character)
	local rs = game:GetService("ReplicatedStorage")
	
	local Hitbox = workspace:GetPartBoundsInBox(Hrp.CFrame * CFrameOffset, Vector3.one * Size)

	local Filtered = {}

	for _, Parts in Hitbox do
		local PossibleCharacter = Parts.Parent

		if PossibleCharacter == Character or table.find(Filtered, PossibleCharacter) or not PossibleCharacter:IsA("Model") then continue end

		local Humanoid = PossibleCharacter:FindFirstChildOfClass("Humanoid")
		if not Humanoid then continue end
		local VfxClone = VfxPart:Clone()
		local ParryVfx = rs.ParryVfx:Clone()

		local PossiblePlayer = game.Players:GetPlayerFromCharacter(PossibleCharacter)
		local leaderstats = PossiblePlayer:WaitForChild("leaderstats")
		local BlockValue = leaderstats.BlockValue
		
		if PossibleCharacter:GetAttribute("IsBlocking") == true and PossibleCharacter:GetAttribute("Parrying") == false and PossibleCharacter:GetAttribute("IsGaurdBroken") == false then
			BlockValue.Value -= BlockDamage
		end
		
		if PossibleCharacter:GetAttribute("IsBlocking") == false and PossibleCharacter:GetAttribute("Parrying") == false and PossibleCharacter:GetAttribute("IsGaurdBroken") == false then
			Humanoid:TakeDamage(Damage)
			VfxClone.CFrame = Humanoid.RootPart.CFrame
			VfxClone.Parent = workspace
			
			PossibleCharacter:SetAttribute("Stunned", true)
			task.wait(1)
			PossibleCharacter:SetAttribute("Stunned", false)
		end
		
		if PossibleCharacter:GetAttribute("IsBlocking") == true and PossibleCharacter:GetAttribute("Parrying") == true and PossibleCharacter:GetAttribute("IsGaurdBroken") == false then
			--local katanaParry = game:GetService("ReplicatedFirst").Animations.KatanaParry
			--local KatanaParryTrack = hum:LoadAnimation(katanaParry)
			--KatanaParryTrack:Play()
			ParryVfx.Parent = workspace
			ParryVfx.CFrame = PossibleCharacter:WaitForChild("Humanoid").RootPart.CFrame
			
		end
		game.Debris:AddItem(ParryVfx,0.5 )

		table.insert(Filtered, PossibleCharacter)
		task.delay(0.5, game.Destroy, VfxClone, ParryVfx)
	end

	table.clear(Filtered)
end

return module

This is because this will return nil if it is not a players character.

local player = game.Players:GetPlayerFromCharacter(Character)

Do something like this

local player = game.Players:GetPlayerFromCharacter(Character)

if player then
    local leaderstats = player:WaitForChild("leaderstats")
    local BlockValue = leaderstats.BlockValue
    -- Block stuff goes here
end

-- rest goes here
1 Like

Thanks alot this worked, just got a quick question, is there anyway to add attributes to an npc?

1 Like

You can add them manually in properties or set them by script

local npc = workspace.NPC

npc:SetAttribute('Attribute', true)
2 Likes

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