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)
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.
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.
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.
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.
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.