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

While True Do Touch Function?

Asked by 2 years ago

Hello, I am trying to make a script that when you press a part, it makes another part rise.

local part = script.Parent
local otherPart = game.Workspace.Water1

local function move()
  while true do
   a()
   wait(2) 
   b()
  end
end

part.Touched:connect(move())

function a() 
for i = 1, 25 do 
wait() 
otherPart.CFrame = otherPart.CFrame * CFrame.new(0, 0.1, 0) 
end 
end 

function b() 
for i = 1, 25 do 
otherPart.CFrame = otherPart.CFrame * CFrame.new(0, -0.1, 0) 
wait() 
end 
end 

This is the script I have tried to use, but isn't working. When the scripts parent is touched by the player, it will execute the move function which will execute the a and b functions which move the part. Could I have help fixing this?

0
maybe remove the brackets in line 12 next to move Diamondultra123 5 — 2y

3 answers

Log in to vote
1
Answered by 2 years ago

Try putting the the local function move() below the end of function b()

0
the whole local function move() Diamondultra123 5 — 2y
0
didnt work AlphaFace1 8 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

The script runs line by line from top to bottom, so when you are starting the loop, a() and b() don't exist yet. An easy fix for this would be to move function a() and function b() above function move(), hope this helps!

0
the part moves, but not on commands when I touch the brick. AlphaFace1 8 — 2y
Log in to vote
0
Answered by
Desmondo1 121
2 years ago

Just a couple notes, your code isn't indented. It took me a while to learn proper formatting for my code but it's extremely important for readability. Basically, any time you're doing anything inside of something else, for example, if statements, you want to indent all the lines in the function, if statement, whatever it is by 4 spaces, you can press tab to do this, and shift plus tab will indent it back 4 spaces

Without formatting

local function()
return 0
end

With formatting

local function()
    return 0
end

It just makes it much easier to work on your code.

Another thing, don't use names like "a" or "b". Use descriptive names, it makes your code much easier to maintain, think about it, if you're working on something for weeks, if you make a function called "a" and you come back to work on it a week later because of a bug, you might've forgotten what that function does, whereas with a descriptive name it wouldn't matter if you forgot. I will note however for prototyping and just testing random stuff it's fine to name your variables and functions stuff like that, because you're not gonna be trying to maintain it.

Now for the problems in the code itself.

  1. Change this line
part.Touched:connect(move())

To this

part.Touched:Connect(move())

Notice how I capitalized the C in "Connect". Almost all functions are named in pascal case (First letter of each word is uppercase)

I don't see anything else wrong in your code. If my changes didn't work, try printing out the part name, are you sure it's the part you want? Or, if it is the part you want, try to start simple, try just moving the part at the start of the game, don't just do a small movement, do a very noticeable one, after that, try adjusting the movement to get it how you like it, after that, try just detecting if the part was touched, after that, try linking up touching the part to the moving action.

Answer this question