Mob Damage only twice

I want to achiewe that my mob is dealing damage every 1 sec he touch the player

Everythinks is working fine but after dealing damage the secound time the dealing damage stops and works only if i move. So if i stand in one place i cant get damage after he hit me 2 times idk why?

My Script

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local DamageMiddle = false

Mob.HitBox.Touched:Connect(function(hit)

	if (Enemy.Health < 1) then return end
	local h = hit.Parent:findFirstChild("Humanoid")	

	if not DamageMiddle and h~=nil then
		DamageMiddle = true
		h:TakeDamage(_M.MobDamage)
		wait(1)
		DamageMiddle = false
	end
end)
1 Like

This is happening because the .Touched event won’t get fired when the two objects aren’t moving at all. You could possible make a while loop that would be every second checking for touching parts (:GetTouchingParts()) and loop through them, but I don’t know how good soloution would this be, but it should possibly work.

2 Likes

Im pretty newbie at scripting and idk how do i use this commands at all “:GetTouchingParts()”
could you help me with that?

GetTouchingParts() is a function of every part. After it being called, it returns a table of parts that are touching with the part that the function is called on.

1 Like

I was giving my best but i still fail what I did wrong?

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local DamageMiddle = false
local MobHit = Mob.HitBox
local h

local function GetTouchingParts(part)
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end

while true do
local res = GetTouchingParts(MobHit)
if res~=nil then
if not DamageMiddle then
DamageMiddle = true
h:TakeDamage(_M.MobDamage)
wait(1)
DamageMiddle = false
end
end
wait(0.1)
end

Mob.HitBox.Touched:Connect(function(hit)


if (Enemy.Health < 1) then return end
h = hit.Parent:findFirstChild("Humanoid")



end)
local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local MobHit = Mob.HitBox

while Enemy.Health < 1 do
    local res = MobHit:GetTouchingParts()
    local didHit = false
    for _, part in pairs(res) do
        local h = part.Parent:FindFirstChild("Humanoid")
        if h then
            h:TakeDamage(_M.MobDamage)
            didHit = true
        end
    end
    if didHit then
        wait(1)
    else
        wait(0.1) -- if you would be getting issues with overloaded server, then make this number bigger
    end
end

I’m not sure if this will work as I haven’t been making it in any script editor, but I think it could work well. Please get back to me after you try this one out.

i dont get any damage + no errors or warnings

Oh sorry, now I see I did a typo over here while Enemy.Health < 1 do there should be while Enemy.Health > 1 do

now the script work but i still dont get any dmg

i did the print check method and the script dont work at for loop line

i was printing “res”
and its always says: {}

so i think this mehtod dont work
local res = MobHit:GetTouchingParts()

How big is the MobHit part? Could you include a picture?

its pretty big because i add a prevention that the mob is runing at you all the time instead of he stops infront of you thats why it is so big

its working but i have to change Hitbox to CanColide = true
but its not the solution i want is there a way to do this witt CanColide = false

Sorry sir, but I’m out of ideas how could you do solve this. If I would be you, I would get back to your old system with using .Touhed until you don’t find some more effective soloution.

1 Like

i mean i like your solution its better than my old thank you anyways :slight_smile:

I found the solution!!!

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local MobHit = Mob.HitBox


local function GetTouchingPartss(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

while Enemy.Health > 1 do
	
	local res = GetTouchingPartss(MobHit)
	local didHit = false
	for _, part in pairs(res) do
		local h = part.Parent:FindFirstChild("Humanoid")
		if h then
			h:TakeDamage(_M.MobDamage)
			didHit = true
		end
	end
	if didHit then
		wait(1)
	else
		wait(0.1) -- if you would be getting issues with overloaded server, then make this number bigger
	end
end


But the problem now is that it hits multiple times
i think is the for loop fault

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local MobHit = Mob.HitBox


local function GetTouchingPartss(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

while Enemy.Health > 1 do
	
	local res = GetTouchingPartss(MobHit)
	local didHit = false
    local affectedHums = {}
	for _, part in pairs(res) do
		local h = part.Parent:FindFirstChild("Humanoid")
		if h and not table.find(affectedHums, h) then
            table.insert(affectedHums, h)
			h:TakeDamage(_M.MobDamage)
			didHit = true
		end
	end
	if didHit then
		wait(1)
	else
		wait(0.1) -- if you would be getting issues with overloaded server, then make this number bigger
	end
end

This should work then. I have added a table where are stored Humanoids that have been damaged. Everytime before the Humanoid gets damaged, the script is checking if it already got damaged.

1 Like

Its works <3 :slight_smile: thank you very much

1 Like

No problem sir, I’m happy to help! But be aware of that this script could fairly lag your game if there would be a lot of the mobs.

1 Like