NPC can't hit anything under it

Hello!

I’m having some issues with my “DamageScript”. I made an function called swing() that fires when the player gets near the monster, and it works, BUT it doesn’t hits anything under it, and since the players ragdolls on low health, the monster cant really hit the player.

local function swing() --Fires when Monster swings (every 1 second)

	for i = -5,5,1 do
		local origin = rootPart.Position
		local lv = rootPart.CFrame.LookVector
		local rv = rootPart.CFrame.RightVector
		local direction = lv * 5 + rv * i
		--local direction = Vector3.new(0, -1, 0)
		local result: RaycastResult? = workspace:Raycast(origin,direction,raycastParams)


		if result then
			local hit: BasePart = result.Instance
			local model = hit:FindFirstAncestorWhichIsA("Model")
			local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")
			if humanoid then
				math.randomseed(tick()) -- Let's make it even more random
				local r = math.random(1,2)
				if humanoid.Health > 35 then
					script.Parent.Torso.Swing:Play()
					script.Parent.Torso.Hit:Play()
					animations[string.format("Swing%s",tostring(r))]:Play()
					humanoid:TakeDamage(config.Damage.Value)
					break
				else
					script.Parent.Torso.HeavyHit:Play()
					animations[string.format("HeavySwing%s",tostring(r))]:Play()
					humanoid:TakeDamage(50)
					break
				end
			end
		end
	end
end

How would I make the monster hit players under him?

In your script, lv and rv are vectors that lie in the XZ plane

Changing your script with this will allow it to hit players below.

local direction = lv * 5 + rv * i - Vector3.new(0, 1, 0)  -- Change 1 to whatever you desire

This will not add anything above though so if players ever go up for whatever reason they might not take damage depending on how far up they go.

1 Like

Well, it did work but he still took some time to damage me, what can I do to fix it? Btw he spins when he’s exactly above me

PS: the delay is 0.75 seconds per hit

If I had to guess the delay is likely due to the time it takes for the server to register the damage and apply it to your character’s health.

local function swing()
    for i = -5,5,1 do
        local origin = rootPart.Position
        local lv = rootPart.CFrame.LookVector
        local rv = rootPart.CFrame.RightVector
        local direction = lv * 5 + rv * i - Vector3.new(0, 1, 0)
        local result: RaycastResult? = workspace:Raycast(origin,direction,raycastParams)

        if result then
            local hit: BasePart = result.Instance
            local model = hit:FindFirstAncestorWhichIsA("Model")
            local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")
            if humanoid then
                math.randomseed(tick())
                local r = math.random(1,2)
                if humanoid.Health > 35 then
                    script.Parent.Torso.Swing:Play()
                    script.Parent.Torso.Hit:Play()
                    animations[string.format("Swing%s",tostring(r))]:Play()
                    spawn(function() -- "spawn" function creates new thread separate from the the main thread
                        humanoid:TakeDamage(config.Damage.Value)
                    end)
                    break
                else
                    script.Parent.Torso.HeavyHit:Play()
                    animations[string.format("HeavySwing%s",tostring(r))]:Play()
                    spawn(function() -- "spawn" function creates new thread from the main thread
                        humanoid:TakeDamage(50)
                    end)
                    break
                end
            end
        end
    end
end

In your original code, the “TakeDamage” call is executed immediately in the main thread of the script. This means that the script has to wait for the server to process the damage before it can continue running, which could be causing the delay.

This updated code the “TakeDamage” call is wrapped in a “spawn” function, which creates a new thread to execute the call. This allows the main thread to continue running immediately without waiting for the server to process the damage. The “TakeDamage” call will still be executed but in a separate thread. Should help reduce the delay if this is the issue.

1 Like

Thank you again, helped me a lot.

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