I have a block where I want it to do damage to the player at a constant rate (like 5 damage a second). Basically, when the player is on the block, the block will remove 5 health from that player each second. I want it so that it will still do damage to the player at a constant rate regardless of whether or not the player is standing still. I would also want the player to be slowed down when the player is on it. How would I do it? Here's what I have so far:
local CollectionService = game:GetService("CollectionService")
local part = CollectionService:GetTagged("DamagePart")
for _, TaggedPart in pairs(part) do
local onblock = false
function onTouched(hit)
if onblock == false then
onblock = true
local h = hit.Parent:findFirstChild("Humanoid")
h.WalkSpeed = 8
while onblock == true do
h.Health = h.Health - 5
wait(1)
end
onblock = false
end
end
function stopdamage(hit)
onblock = false
hit.Parent:findFirstChild("Humanoid").WalkSpeed = 16
end
TaggedPart.Touched:Connect(onTouched)
TaggedPart.TouchEnded:Connect(stopdamage)
end
This is quite buggy and does inconsistent damage. It also will sometimes not do damage when the player is standing still. If you could tell me what I did wrong and paste a working code snippet in I would really appreciate it. Thanks for your help :)
Also while you're at it can you please answer this question for me too? scriptinghelpers.org/questions/115188/bullets-are-not-going-through-objects-without-collisions

1 comment
Thanks! I moved the wait(1) line after the if statement so that way players can't avoid taking damage by jumping. Other than that it works as expected :)