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

How do I make a brick slowly kill a player?

Asked by 7 years ago
Edited 7 years ago

I'm trying to build a brick that when you touch it it only takes some damage but will continue to that same amount every few seconds while the player is on the brick.

1function onTouched(hit)
2local h = hit.Parent:findFirstChild("Humanoid")
3if (h ~= nil) then
4h.Health = h.Health - 6
5wait(3)
6end
7end
8 
9script.Parent.Touched:connect(onTouched)

I tried this script but it will not continue to take damage when the player is standing still.

0
So you want me to hurt you just once? imagYTOP 19 — 7y

2 answers

Log in to vote
2
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
7 years ago
Edited 7 years ago

The above code works but in terms of efficiency and professionalism, it completely lacks that. Since using Loops is an important skills as suggested by theCJarmy7, I will be teaching you basics of it and teach you how to implement it to other codes.

What is loops?

Loops are basically a set of code that repeats either certain amount of times, or infinitive. There are particularly three types of Loops

1) While true do Loop

2) For Loop

3) Repeat..Until Loop

1) What is While true do loop ?

A While True Do loop is a type of loop that runs a set of code infinite times until the condition is false. Here is how you would loop your code using the While true do.

Warning! Always end an infinite loop or add a wait() or else the code will crash your game!

01function onTouched(hit)
02    local h = hit.Parent:findFirstChild("Humanoid")
03    while true do
04        if (h ~= nil) then
05            h.Health = h.Health - 6
06            wait(3)
07        end
08    end
09end
10 
11script.Parent.Touched:connect(onTouched)

2) What is For loop ?

For loop is another type of loop which, unlike while true do loop, it only runs the code certain times, explicitly defined by the programmer. I will take the example using MrLordFearLT's code.

01local function PlayerTouched(Part)
02    local Parent = Part.Parent
03    if game.Players:GetPlayerFromCharacter(Parent) then
04    for number = 100, 0, -10 do
05        Parent.Humanoid.Health = number
06        wait(3)
07     end
08    end
09end
10 
11Brick.Touched:connect(PlayerTouched)
12 
13-- Look at that! We wrote that long code in such short place.

3) What is Repeat Loop ?

Repeat loop is the last type of the loops which repeats the code block until the condition has been met. Again, let's use your code as an example.

1function onTouched(hit)
2    local h = hit.Parent:findFirstChild("Humanoid")
3    repeat
4        h.Health = h.Health - 6
5        wait(3)
6    until h.Health == 0
7end
8 
9script.Parent.Touched:connect(onTouched)

Hope you learned something from this.

Have a lovely day of coding
Ad
Log in to vote
-1
Answered by 7 years ago

Hi There Im not best at scripting im beginner too My Roblox Username MrLordFearLT if you want to invite me:D here i builded script for you u can change lines where wait(number) and how much player will have health per second Script --Variables-- local Brick = script.Parent --End--

--Code--

01local function PlayerTouched(Part)
02    local Parent = Part.Parent
03    if game.Players:GetPlayerFromCharacter(Parent) then
04        Parent.Humanoid.Health = 90
05        wait(1)
06        Parent.Humanoid.Health = 80
07        wait(1)
08        Parent.Humanoid.Health = 70
09        wait(1)
10        Parent.Humanoid.Health = 60
11        wait(1)
12        Parent.Humanoid.Health = 50
13        wait(1)    
14        Parent.Humanoid.Health = 40
15        wait(1)
View all 27 lines...

Insert In part Named Part and add in Part Script and copy this script to Script Document.. Have A good day! u can response me if you want.

0
i said im beginner i created this what i know:D User#21499 0 — 7y
1
your script is inefficient DaWarTekWizard 169 — 7y

Answer this question