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

How do I use a While Loop to wait for someone to say "reset"?

Asked by
Djinous 45
9 years ago

I have a code here where when you step on a block, it diminishes in size until it goes away completely, and then collision turns off. It stays like that, always. What I want to do is append it to where if someone says "reset", it will perform the code after that part. I've added a while loop in the middle, but I don't know what to put inside it.

001local debounce = true
002local d = script.Parent.Decal
003t = 0.05
004 
005function onTouched(hit)
006if (debounce == true) then
007    debounce = false
008    wait(t)
009    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
010    wait(t)
011    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
012    wait(t)
013    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
014    wait(t)
015    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
View all 107 lines...
1
Do you know what a for loop is? GoldenPhysics 474 — 9y
0
I believe so. As long as this is true, that will happen? Djinous 45 — 9y
0
I've added a while loop to the code, as I now know how to get it to repeatedly check for something, but I don't know what to put in there. Djinous 45 — 9y

3 answers

Log in to vote
3
Answered by
DevSean 270 Moderation Voter
9 years ago

Firstly, instead of having the following code 20 times you should use a for loop:

1wait(t)
2 script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x+0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z+0.05)

Like this:

1for i=1, 20 do -- this bit loops 20 times
2    wait(t)
3    mesh.Scale = mesh.Scale - Vector3.new(0.05, 0, 0.05)
4end

It's also simpler to add/subtract the mesh scale like this:

1mesh.Scale = mesh.Scale - Vector3.new(0.05, 0, 0.05) -- decrease X & Z
2 
3mesh.Scale = mesh.Scale + Vector3.new(0.05, 0, 0.05) -- increase X & Z

A while loop is unnecessary as you can just call a reset function when someone chats.

To connect to a players chat you should use chatted

1game.Players.PlayerAdded:connect(function(player)
2    player.Chatted:connect(function(msg)
3        -- do stuff with msg and player
4    end)
5end)

Here's your amended code using the player.Chatted: At line 21 I've used CanCollide as another debounce so users cannot activate the reset function while it is currently active.

01local debounce = true
02local d = script.Parent.Decal
03local mesh = script.Parent.Mesh
04t = 0.05
05 
06function onTouched(hit)
07    if (debounce == true) then
08        debounce = false
09        for i=1, 20 do
10            wait(t)
11            mesh.Scale = mesh.Scale - Vector3.new(0.05, 0, 0.05)
12        end
13 
14        script.Parent.CanCollide = false
15        script.Parent.Transparency = 1
View all 44 lines...

If there are any errors or problems, feel free to ask any questions!

0
This is perfect! Thank you! Djinous 45 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Dev beat me to it, but I thought I'd give you this alternative, it's a little different so you're bound to learn at least something from it.

01local Decal = script.Parent.Decal
02local Mesh = script.Parent.Mesh
03local Animating = false
04local Debounce = false
05 
06local AnimateSpeed = 100
07local Max_X, Max_Y, Max_Z = 1, 1, 1 -- The "reset" size of the Mesh
08 
09local function TransformMesh(Direction) -- We'll say true = out and false = in
10    Debounce = true
11    Animating = true
12    for Index = 1, AnimateSpeed do
13        Mesh.Scale = Vector3.new(
14            (Direction and 0 or Max_X) + (Direction and ((Max_X/AnimateSpeed) * Index) or -((Max_X/AnimateSpeed) * Index)),
15            Max_Y, -- (Direction and 0 or Max_Y) + (Direction and ((Max_Y/AnimateSpeed) * Index) or -((Max_Y/AnimateSpeed) * Index)),
View all 37 lines...
Log in to vote
0
Answered by 9 years ago

The obvious is question is why you're not using for loops?

Answer this question