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:

01local CollectionService = game:GetService("CollectionService")
02local part = CollectionService:GetTagged("DamagePart")
03 
04for _, TaggedPart in pairs(part) do
05    local onblock = false
06    function onTouched(hit)
07        if onblock == false then
08            onblock = true
09            local h = hit.Parent:findFirstChild("Humanoid")
10            h.WalkSpeed = 8
11            while onblock == true do
12                h.Health = h.Health - 5
13                wait(1)
14            end
15            onblock = false
View all 25 lines...

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:

01local human = script.Parent:FindFirstChild("Humanoid")
02 
03if human then
04    while true do
05        wait(1)
06        if script.Parent.TouchingDamageBlock.Value == true then
07            human.Health = human.Health - 5
08        end
09    end
10end

Then, put this script in your block.

01script.Parent.Touched:Connect(function(hit)
02    local human = hit.Parent:FindFirstChild("Humanoid")
03    if human then
04        hit.Parent.TouchingDamageBlock.Value = true
05    end
06end)
07 
08script.Parent.TouchEnded:Connect(function(hit)
09    local human = hit.Parent:FindFirstChild("Humanoid")
10    if human then
11        hit.Parent.TouchingDamageBlock.Value = false
12    end
13end)
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