Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Make Block Constantly Damage Player on Touch?

Asked by 4 years ago

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 answer

Log in to vote
0
Answered by 4 years ago

Put a boolean value into StarterPlayer > StarterCharacterScripts. Name it TouchingDamageBlock or something like that. Now, put a script in StarterCharacterScripts. In the script write:

local human = script.Parent:FindFirstChild("Humanoid")

if human then
    while true do
        wait(1)
        if script.Parent.TouchingDamageBlock.Value == true then
            human.Health = human.Health - 5
        end
    end
end

Then, put this script in your block.

script.Parent.Touched:Connect(function(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human then
        hit.Parent.TouchingDamageBlock.Value = true
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human then
        hit.Parent.TouchingDamageBlock.Value = false
    end
end)
0
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 :) Bob4koolest 78 — 4y
Ad

Answer this question