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

How do I have a table find all player's touching a part then run a function on them til they leave?

Asked by 5 years ago

I'm making this script that spawns a radius which has it's own little animation then when it touches any other player they start taking 10 damage every second until they stop touching it or they die. If the player is the player that made it then they start gaining 10 health every second until they stop touching it. Most of the stuff work but the issue I'm having is allowing the script to find multiple players. Like although it does find other players it finds them like at the very end of the script and once the player is dead. So in output it's the first player that was touched name spammed a large amount of time and once they die another player's name appears. I've tried many things to try to fix this but I just cant get it.

game.Workspace.RosadaRemote:FindFirstChild("Garden").OnServerEvent:Connect(function(Player, Debounce)
    local deb = false
    local deb2 = false
    local AlreadyTouched = false
    local Characterl = Player.Character or Player.CharacterAdded:Wait()
    Debounce = true
    local Garden = game:GetService("ReplicatedStorage"):FindFirstChild("Garden")
    local GardenO = Garden:Clone()
    GardenO.Name = "Garden"
    GardenO.Parent = game.Workspace
    GardenO.CFrame = Characterl.HumanoidRootPart.CFrame*CFrame.new(0,-9,0)* CFrame.Angles(0,0,math.pi/-2)
    local TweenService = game:GetService("TweenService")
    local Info = TweenInfo.new(1.5,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0)
    local Goals = {CFrame = Characterl.HumanoidRootPart.CFrame*CFrame.new(0,-5,0)* CFrame.Angles(0,0,math.pi/-2);}
    local Goals2 = {Transparency = 0;CFrame = Characterl.HumanoidRootPart.CFrame*CFrame.new(0,-9,0)* CFrame.Angles(0,0,math.pi/-2);}
    local Tween = TweenService:Create(GardenO,Info,Goals)
    local Tween2 = TweenService:Create(GardenO,Info,Goals2)
    Tween:Play()
    local BV = Instance.new("BodyVelocity")
    BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    BV.Velocity = Characterl.HumanoidRootPart.CFrame.lookVector*0
    BV.Parent = GardenO

    local PlayersInRadius = {}
    local CreatorInRadius = false
    local IsInRadius = false

GardenO.Touched:Connect(function()
    for i,v in pairs(GardenO:GetTouchingParts()) do
        if v:IsA("BasePart")then
            if GardenO.TouchEnded then
                IsInRadius = false
            end
            if v.Parent:FindFirstChild("Humanoid") ~= nil then
                IsInRadius = true
                local Character = v.Parent
                if not PlayersInRadius[Character.Name]then
                table.insert(PlayersInRadius, Character.Name)
                print(unpack(PlayersInRadius))
                if v.Parent:FindFirstChild("Humanoid").Health == 0 then
                    IsInRadius = false
                end
                if Character == Characterl then
                    repeat
                    if IsInRadius then
                    if deb == true then return end
                    deb = true
                    CreatorInRadius = true
                    Character.Humanoid.Health = Character.Humanoid.Health + 10
                    print("Healed "..Character.Name.." Once")
                    wait(1)
                    deb = false
                    end
                    until
                    IsInRadius == false
                    else
                    repeat
                    if IsInRadius then
                        if deb2 == true then return end
                        deb2 = true
                        Character.Humanoid:TakeDamage(10)
                        print("Damaged "..Character.Name.." Once")
                        wait(1)
                        deb2 = false
                    end
                    until
                    IsInRadius == false
                    end
                 end
             end
         end
      end
      wait(5)
      Tween2:Play()
      wait(1.5)
      GardenO:Destroy()
end)
wait(0.2)
Debounce = false
end)
0
Bump songboy50 77 — 5y

Answer this question