Clicking a button makes the player slowly die

i already have this !!WORKING!! script for a sound and particles that play. i only need it so if you press the parent part the player slowly gets damaged and eventually dies. ((no need to change the particle and sound script))

script.Parent.ClickDetector.MouseClick:Connect(function(plr) 
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v
         end
     end
end)
1 Like
script.Parent.ClickDetector.MouseClick:Connect(function(plr) 
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v
         end
     end
     for i = 1, 20, 1 do
         plr.Character.Humanoid:TakeDamage(5)
         wait(1)
     end
end)
1 Like

was making me get damage but stopped after awhile. making me stay alive

local rate = 10 -- measured in health / s; player loses 10 health per second

script.Parent.ClickDetector.MouseClick:Connect(function(plr) 
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v

            while (plr.Character.Humanoid.Health > 0) do
                local wt = wait()
                plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
            end
         end
     end
end)
1 Like

this did it but made the particles only appear at the torso.

Here’s the fix:

local rate = 10 -- measured in health / s; player loses 10 health per second

script.Parent.ClickDetector.MouseClick:Connect(function(plr) 
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v
         end
     end
     while (plr.Character.Humanoid.Health > 0) do
         local wt = wait()
         plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
     end
end)

I realised that the loop just yields the thread until it is finished, making it only parent it in the torso and when the player dies, it’ll insert it into the other body parts. Putting the while loop after inserting the particles into all of their parts should work

4 Likes

thanks for helping, again. also you @nuttela_for1me im only going to need one more part to this script and i will make the new thread in a few sec.

Hey, Raretendoblox I was just thinking what would happen if many players would press this part or a player would click on part many times, wouldnt the system fail? Should we use the spawn function? I have never used it, thats why I ask.

or maybe a cooldown? (30 charac)

Well, if that’s the case, it’ll insert many particle emitters into all players and making more while loops taking their health away, thus increasing the rate. What can be done to counteract this is to just implement a cooldown / debounce for a set amount of time, like probably wait until all players die, which should take 10 seconds if the rate is consistently at 10 health per second.

spawn allows code to be run almost simultaneously, but I wouldn’t use spawn for this reason – spawn would be used to make all players die simultaneously because the while loop will just wait until the player dies, then a player one after the after

If OP wishes to implement a cooldown:

local deb = false
local rate = 10 -- measured in health / s; player loses 10 health per second

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
     if not deb then
     deb = true
     script.Parent.woosh.SoundId = "rbxassetid://1839756886"
     script.Parent.woosh:Play()
     for i, v in pairs(plr.Character:GetDescendants()) do
         if  v:IsA("BasePart") then
            local particle = script.Parent.ParticleEmitter:Clone()
            particle.Enabled = true
            particle.Parent = v
         end
     end
     spawn(function()
     while (plr.Character.Humanoid.Health > 0) do
         local wt = wait()
         plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - (wt * rate)
     end
     end)
     end
     wait(100 / rate) -- if you ever decide to change the rate
     deb = false
     end
end)