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
8 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.

local debounce = true
local d = script.Parent.Decal
t = 0.05

function onTouched(hit)
if (debounce == true) then
    debounce = false
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)

    script.Parent.CanCollide = false
    script.Parent.Transparency = 1
    d.Parent = nil
    while (1 == 1)
{
    if --Someone says "reset"
}
    script.Parent.CanCollide = true
    script.Parent.Transparency = 0
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)

    d.Parent = script.Parent
    script.Parent.CanCollide = true
    script.Parent.Mesh.Scale = Vector3.new(1,1,1)
    script.Parent.Transparency = 0
    debounce = true
end
end

connection = script.Parent.Touched:connect(onTouched)
1
Do you know what a for loop is? GoldenPhysics 474 — 8y
0
I believe so. As long as this is true, that will happen? Djinous 45 — 8y
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 — 8y

3 answers

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

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

wait(t)
 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:

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

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

mesh.Scale = mesh.Scale - Vector3.new(0.05, 0, 0.05) -- decrease X & Z

mesh.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

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        -- do stuff with msg and player
    end)
end)

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.

local debounce = true
local d = script.Parent.Decal
local mesh = script.Parent.Mesh
t = 0.05

function onTouched(hit)
    if (debounce == true) then
        debounce = false
        for i=1, 20 do
            wait(t)
            mesh.Scale = mesh.Scale - Vector3.new(0.05, 0, 0.05)
        end

        script.Parent.CanCollide = false
        script.Parent.Transparency = 1
        d.Parent = nil
    end
end

function reset()
    if ((debounce == false) and (not script.Parent.CanCollide)) then -- make sure we're not already doing this...
        script.Parent.CanCollide = true
        script.Parent.Transparency = 0

        for i=1, 20 do
            wait(t)
            mesh.Scale = mesh.Scale + Vector3.new(0.05, 0, 0.05)
        end

        d.Parent = script.Parent
        script.Parent.Mesh.Scale = Vector3.new(1,1,1)
        debounce = true
    end 
end

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        if (string.lower(msg) == "reset") then
            reset()
        end
    end)
end)

connection = script.Parent.Touched:connect(onTouched)

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

0
This is perfect! Thank you! Djinous 45 — 8y
Ad
Log in to vote
0
Answered by 8 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.

local Decal = script.Parent.Decal
local Mesh = script.Parent.Mesh
local Animating = false
local Debounce = false

local AnimateSpeed = 100
local Max_X, Max_Y, Max_Z = 1, 1, 1 -- The "reset" size of the Mesh

local function TransformMesh(Direction) -- We'll say true = out and false = in
    Debounce = true
    Animating = true
    for Index = 1, AnimateSpeed do
        Mesh.Scale = Vector3.new(
            (Direction and 0 or Max_X) + (Direction and ((Max_X/AnimateSpeed) * Index) or -((Max_X/AnimateSpeed) * Index)),
            Max_Y, -- (Direction and 0 or Max_Y) + (Direction and ((Max_Y/AnimateSpeed) * Index) or -((Max_Y/AnimateSpeed) * Index)),
            (Direction and 0 or Max_Z) + (Direction and ((Max_Z/AnimateSpeed) * Index) or -((Max_Z/AnimateSpeed) * Index))
        )
        wait()
    end
    script.Parent.CanCollide = Direction
    Animating = false
end

game.Players.PlayerAdded:connect(function(Player)
    Player.Chatted:connect(function(Message)
        if Message:lower() == "reset" and not Animating then
            Debounce = false
            TransformMesh(true)
        end
    end)
end)

script.Parent.Touched:connect(function()
    if not Debounce then
        TransformMesh(false)
    end
end)
Log in to vote
0
Answered by 8 years ago

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

Answer this question