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.
001 | local debounce = true |
002 | local d = script.Parent.Decal |
003 | t = 0.05 |
004 |
005 | function onTouched(hit) |
006 | if (debounce = = true ) then |
007 | debounce = false |
008 | wait(t) |
009 | script.Parent.Mesh.Scale = Vector 3. 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 = Vector 3. 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 = Vector 3. 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 = Vector 3. new(script.Parent.Mesh.Scale.x- 0.05 ,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z- 0.05 ) |
Firstly, instead of having the following code 20 times you should use a for loop
:
1 | wait(t) |
2 | script.Parent.Mesh.Scale = Vector 3. new(script.Parent.Mesh.Scale.x+ 0.05 ,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z+ 0.05 ) |
Like this:
1 | for i = 1 , 20 do -- this bit loops 20 times |
2 | wait(t) |
3 | mesh.Scale = mesh.Scale - Vector 3. new( 0.05 , 0 , 0.05 ) |
4 | end |
It's also simpler to add/subtract the mesh scale like this:
1 | mesh.Scale = mesh.Scale - Vector 3. new( 0.05 , 0 , 0.05 ) -- decrease X & Z |
2 |
3 | mesh.Scale = mesh.Scale + Vector 3. 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
1 | game.Players.PlayerAdded:connect( function (player) |
2 | player.Chatted:connect( function (msg) |
3 | -- do stuff with msg and player |
4 | end ) |
5 | 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.
01 | local debounce = true |
02 | local d = script.Parent.Decal |
03 | local mesh = script.Parent.Mesh |
04 | t = 0.05 |
05 |
06 | function onTouched(hit) |
07 | if (debounce = = true ) then |
08 | debounce = false |
09 | for i = 1 , 20 do |
10 | wait(t) |
11 | mesh.Scale = mesh.Scale - Vector 3. new( 0.05 , 0 , 0.05 ) |
12 | end |
13 |
14 | script.Parent.CanCollide = false |
15 | script.Parent.Transparency = 1 |
If there are any errors or problems, feel free to ask any questions!
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.
01 | local Decal = script.Parent.Decal |
02 | local Mesh = script.Parent.Mesh |
03 | local Animating = false |
04 | local Debounce = false |
05 |
06 | local AnimateSpeed = 100 |
07 | local Max_X, Max_Y, Max_Z = 1 , 1 , 1 -- The "reset" size of the Mesh |
08 |
09 | local 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 = Vector 3. 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)), |