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

How do I make a part damage a player for every second that the player is touching the part?

Asked by 5 years ago

I have a radius that when it's touched by the creator its supposed to heal the player by 10 health for every second that the player is still touching the garden, and if they aren't the creator it does the same thing except it hurts the player. My script isn't working, what am I doing wrong?

GardenO.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChild("Humanoid")

    if Humanoid == nil then return end

        if Humanoid.Parent == Character then
            if deb == true then return end
            deb = true
            print("Local Humanoid Touched")
            pcall(function()
            Humanoid.Health = Humanoid.Health+10
            wait(1)
            end)
        else
            if deb2 == true then return end
            deb2 = true
            print("Humanoid Touched")
            pcall(function()
            Humanoid.Health = Humanoid.Health-10
            wait(1)
            end)
        end
        wait(5)
        GardenO:Destroy()
        deb = false
        deb2 = false
    end)
0
this won't work if multiple players are touching the part hellmatic 1523 — 5y

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
5 years ago

You can achieve this by using tables and loops:

local PlayersInRadius = {}

local CreatorInRadius = false 

local function runDamageOnPlayer(player)
    while player and PlayersInRadius[player.Name] do 
        wait(1)
        local Character = player.Character
        if Character then 
            local IsInRadius = false 
            local PartsTouching = Character.HumanoidRootPart:GetTouchingParts()
            -- GetTouchParts() can be called on a BasePart. It returns a table of parts that collide or intersect with the given BasePart.
            for i, part in pairs(PartsTouching) do 
                if part == Garden0 then -- match part with 'Garden0'
                    IsInRadius = true -- set bool to true
                    Character.Humanoid:TakeDamage(10) -- You can use the TakeDamage() function instead. Remember that player's with a force field won't take any damage using this method.
                end
            end
            if not IsInRadius then 
                -- Cancels the loop if 'IsInRadius' bool is false 
                break
            end
        else
            -- Cancels the loop if 'Character' is nil
            break
        end
    end
    -- Note that any code below a loop will not run unless the loop was canceled using 'break' or wrapped in a function (spawn, delay, etc..)
    PlayersInRadius[player.Name] = nil -- Set stored value of the player to nil (similar to table.remove)
end

local function runHealingForCreator()
    while CreatorCharacter and CreatorInRadius do 
        wait(1)
        local IsInRadius = false 
        local PartsTouching = CreatorCharacter.HumanoidRootPart:GetTouchingParts()
        for i, part in pairs(PartsTouching) do 
            if part == Garden0 then 
                IsInRadius = true 
                Character.Humanoid.Health = Character.Humanoid.Health + 10
            end
        end
        if not IsInRadius then 
            break
        end
    end
    CreatorInRadius = false 
end

Garden0.Touched:Connect(function(part)
    local Humanoid = part.Parent:FindFirstChild("Humanoid")
    if Humanoid then 
        local PlayerTouched = game.Players:GetPlayerFromCharacter(part.Parent)
        -- GetPlayerFromCharacter() has one argument which is the player's character model and returns the player
        if PlayerTouched then 
            if not CreatorInRadius and CreatorCharacter == part.Parent then 
                CreatorInRadius = true 
                runHealingForCreator()
                return
            end
            if not PlayersInRadius[PlayerTouched.Name] then 
                PlayersInRadius[PlayerTouched.Name] = PlayerTouched -- Stores the player in table
                runDamageOnPlayer(PlayerTouched)
            end
        end
    end
end)
0
Didn't test this (not at home atm) but lmk if there's any errors or if it worked. hellmatic 1523 — 5y
0
You solved my two big issues with my script without me even asking for the other one. Thank you. songboy50 77 — 5y
Ad

Answer this question