Hi, I’ve created a block that does continuous damage while standing OR walking on top of it, based on the Touch event (instead of the other option, with “GetPartBoundsInBox” I believe). It works fairly okay, especially when comparing it to what I’ve found online.
Full script
local part = script.Parent
local PlayersDamaging = {}
part.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
if not table.find(PlayersDamaging, hit.Parent) then
table.insert(PlayersDamaging, hit.Parent)
local jumpold = hum.JumpPower
local emitter = game.ServerStorage.LavaParticles:Clone()
emitter.Parent = hit.Parent.Head
coroutine.resume(coroutine.create(function()
while table.find(PlayersDamaging, hit.Parent) do
local touches = part:GetTouchingParts()
hum:TakeDamage(0.15)
hum.JumpPower = 0
hit.Parent.Health.Disabled = true
if not table.find(touches, hit.Parent) and hum.MoveDirection.Magnitude == 0 then
game["Run Service"].Heartbeat:Wait()
if not table.find(touches, hit.Parent) and hum.MoveDirection.Magnitude ~= 0 then
hum.JumpPower = jumpold
hit.Parent.Health.Disabled = false
emitter:Destroy()
table.remove(PlayersDamaging, table.find(PlayersDamaging, hit.Parent))
end
end
game["Run Service"].Heartbeat:Wait()
end
end))
end
end
end)
Players do get continuously damaged, by both walking and standing on the block. Below is the ‘difficult’ part.
Difficult part
if not table.find(touches, hit.Parent) and hum.MoveDirection.Magnitude == 0 then
game["Run Service"].Heartbeat:Wait()
if not table.find(touches, hit.Parent) and hum.MoveDirection.Magnitude ~= 0 then
First off, it means that players who stand still have an extra heartbeat before getting damaged (which is fine I guess, they don’t regenerate health anyway). That wait is however necessary, because I’m first testing someone standing still, and then walking.
The first if includes the table.find argument which is true when standing still, both on and off the lava.
The second if includes the table.find argument which can only be true when walking off the lava. The thing is, this is quite inconsistent. You need to wiggle a bit when you’re off the block, before losing the effect. If I just remove the first condition, the effect does not longer work when walking on top of the block.
Splitting the two apart (when walking and when standing still) didn’t work for me, although there may be a possibility I have overlooked. The loop sometimes doesn’t close and the emitter and jumppower get messed up. Here it is below.
Not-working solution
local part = script.Parent
local PlayersDamaging = {}
local deb = true
part.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
if not table.find(PlayersDamaging, hit.Parent) then
table.insert(PlayersDamaging, hit.Parent)
local jumpold = hum.JumpPower
local emitter = game.ServerStorage.LavaParticles:Clone()
emitter.Parent = hit.Parent.Head
coroutine.resume(coroutine.create(function()
while table.find(PlayersDamaging, hit.Parent) do
local touches = part:GetTouchingParts()
hum:TakeDamage(0.1)
hum.JumpPower = 0
hit.Parent.Health.Disabled = true
if not table.find(touches, hit.Parent) and hum.MoveDirection.Magnitude ~= 0 then
hum.JumpPower = jumpold
hit.Parent.Health.Disabled = false
emitter:Destroy()
table.remove(PlayersDamaging, table.find(PlayersDamaging, hit.Parent))
end
print("Check1")
game["Run Service"].Heartbeat:Wait()
end
end))
end
end
end)
part.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
if deb == true then
deb = false
jumpold = hum.JumpPower
emitter = game.ServerStorage.LavaParticles:Clone()
emitter.Parent = hit.Parent.Head
end
hum:TakeDamage(3)
hum.JumpPower = 0
hit.Parent.Health.Disabled = true
print("Check2")
game["Run Service"].Heartbeat:Wait()
print(jumpold)
hum.JumpPower = jumpold
hit.Parent.Health.Disabled = false
emitter:Destroy()
deb = true
end
end)
Any ideas on how to get this consistent? An idea using GetPartBoundsInBox or even Region3 is welcome as well if that works of course.