|
| 29 Sep 2014 04:41 AM |
| Hello. I have no clue how to script. I really only know bare minimum, i'm trying to make a simple sliding door. The catch is that im trying to make it so that a button opens and closes the door AND the decal on the button changes when i click the button. So when its locked it has a "Locked" decal and when unlocked it has an "Unlocked" decal. Anything helps. Thank you for your time :) |
|
|
| Report Abuse |
|
|
lama321
|
  |
| Joined: 18 Dec 2011 |
| Total Posts: 300 |
|
|
| 29 Sep 2014 07:42 AM |
> Firstly, you must insert a ClickDetector into the button > The ClickDetector has an event which fires whenever it is clicked, called 'MouseClick'
> That is if you put the following code into a script inside the ClickDetector, it will print "The button has been clicked" whenever the button is clicked
------CodeSegment--------- function onClicked() print("The button has been clicked") end
script.Parent.MouseClick:connect(onClicked) --------------------------
> To be able to script a sliding door, you must also understand variables and if statements > For example, look at the following code
------CodeSegment--------- doorIsOpen = false
function onClicked() if doorIsOpen == true then doorIsOpen = false else doorIsOpen = true end end
script.Parent.MouseClick:connect(onClicked) --------------------------
> This means that everytime you click the button, if the doorIsOpen value is true, it is set to false, and if it is false it is set to true
> Finally, you must have an understanding of for loops and CFraming > Take a look at the following code
------CodeSegment--------- for i = 1, 10 do print(i) wait(1) end print("Loop is Finished") --------------------------
> This is a for loop, where the code between the first line and the end is repeated over and over, with each time it is repeated 1 is added to the value of 'i' > When the value of 'i' is equal to '10' the code will stop repeating, and 'Loop is Finished' will be printed
> CFraming is possible the hardest thing to understand in the creation of a sliding door > Firstly, you must understand that a CFrame is simply a matrix of 9 numbers. For example, if you insert a part into the Workspace, then place the following code inside it, the part's CFrame will be printed
------CodeSegment--------- print(script.Parent.CFrame) --------------------------
> The first three numbers are the only ones which we are concerned with, as these three numbers give the position of the object > The remaining 6 numbers give the rotation of the part, and are not used when scripting a sliding door
>Now, it stands to reason that if we can add a small amount to the first 3 values of the CFrame over and over again every tenth of a second we can change a parts position in such a way that it resembles smooth movement > We do this with the following code (assuming this code is in a part)
------CodeSegment--------- for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(1, 0, 0) wait(0.1) end --------------------------
> This adds 1 to the first number of the part's CFrame, which is the parts x-coordinate
> Now, if we add everything we have learnt together we get the following code, which should work so long as the script is in a clickdetector, which is a child of the button, which is in the same model as the door
------CodeSegment--------- doorIsOpen = false door = script.Parent.Parent.Door -- Change this to a reference to the door
function onClicked() if doorIsOpen == true then for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(1, 0, 0) wait(0.1) end doorIsOpen = false else for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(-1, 0, 0) wait(0.1) end doorIsOpen = true end end
script.Parent.MouseClick:connect(onClicked) --------------------------
> Not it might help your understanding if you paste this code into a script and indent it so that it is easier to read
> Now there is 1 more problem that we have to overcome, and that is that if you double click the button before the for loop has finished, a new for loop will be created and the part will continue to slide right/left for longer than it should > To prevent this we can add 'Debounce', which is a fancy word for a variable which prevents the function running while the function is already being run
> Look at this simple code
------CodeSegment--------- debounce = true
function onClicked() if debounce == true then debounce = false for i=1, 10 do print(i) wait(0.1) end debounce = true end end
script.Parent.MouseClick:connect(onClicked) --------------------------
> Now this code makes sure that the code inside the if statement is not ever running more than one time
> So if we add this into our sliding door script we get the complete script
------CodeSegment--------- doorIsOpen = false debounce = true door = script.Parent.Parent.Door -- Change this to a reference to the door
function onClicked() if debounce == true debounce = false if doorIsOpen == true then for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(1, 0, 0) wait(0.1) end doorIsOpen = false else for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(-1, 0, 0) wait(0.1) end doorIsOpen = true end wait(0.5) debounce = true end end
script.Parent.MouseClick:connect(onClicked) --------------------------
> And there you have it, a sliding door script
(note that I have not described the changing of the Decals on the button, but you basically have to change their Transparency properties at the same time as you change the doorIsOpen value and with some experimentation, you will have your sliding door in no time)
I apologise for any code errors in this post, i'm afraid i haven't tested any scripts but i think they should probably all work fine. |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2014 07:58 AM |
Thank you so much for spending your time to try and help me. Im tweaking the script trying to make it work. The problem may be that the door is a model, because it is multiple parts, and i could not make it a union for some reason. Upon trying the script ive had no success, but i notice under "debounce = false" there is a red line and when i hover over it, it says "Expected 'then', got 'debounce'" and ive got no clue what that means. Also since the door is a model i tried to do this:
door = game.Workspace.B1.Door:GetChildren() -- Change this to a reference to the door
Which i thought would work, guess not. If you see where ive gone wrong please tell me? This is how im using the script right now:
doorIsOpen = false debounce = true door = game.Workspace.B1.Door.Part -- Change this to a reference to the door
function onClicked() if debounce == true debounce = false if doorIsOpen == true then for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(1, 0, 0) wait(0.1) end doorIsOpen = false else for i=1, 20 do script.Parent.CFrame = script.Parent.CFrame + Vector3.new(-1, 0, 0) wait(0.1) end doorIsOpen = true end wait(0.5) debounce = true end end
script.Parent.MouseClick:connect(onClicked)
------------------
Im sure im just doing something wrong on my part, thanks for the help though |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2014 08:08 AM |
tl;dr
make a part with transparency of 0, and put a script and clickdetector inside the part. put this into the script:
Debounce = false function clicky() if (Debounce = false) then Debounce = true script.Parent.CanCollide = false script.Parent.Transparency = .5 else Debounce = false script.Parent.CanCollide = true script.Parent.Transparency = 0 end script.Parent.ClickDetector.MouseClick:connect(clicky)
no need to thank me
|
|
|
| Report Abuse |
|
|
|
| 29 Sep 2014 08:09 AM |
wait you said sliding door
mmm, it depends whether your door is on the x or z axis
gg |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2014 08:23 AM |
| Found out how to move one part of the door, like i said in my last reply. The door is a model. I had to move (0,0,1) So the z axis? |
|
|
| Report Abuse |
|
|
lama321
|
  |
| Joined: 18 Dec 2011 |
| Total Posts: 300 |
|
|
| 29 Sep 2014 06:49 PM |
ha, i forgot a then,
To fix the debounce problem you just need to change
> if debounce == true
to
> if debounce == true then |
|
|
| Report Abuse |
|
|
lama321
|
  |
| Joined: 18 Dec 2011 |
| Total Posts: 300 |
|
|
| 29 Sep 2014 07:05 PM |
> As for the door being a model, there are 2 things you can do > Firstly you must understand that :GetChildren() returns a table of all the children of the model, and so it stands to reason that you can go through each one of these children and move them each slightly > To do this we must understand tables
> To access values in a table we use a special kind of loop that looks like this
-----------Code Snippet----------- for i, v in pairs(nameoftable) do print(v) wait(0.1) end ---------------------------------
> As you can see from this code, the first time this loop runs, i = 1, and v = the first value in the table > The second time the loop runs, i = 2, v = the second value in the table > And So On
> Now all you have to do is simple change the CFrame of v as below
-----------Code Snippet----------- for i, v in pairs(nameoftable) do v.CFrame = v.CFrame + Vector3.new(0, 0, 1) end ---------------------------------
> This code will add 1 to the Z-Coordinate of every element of the table nameoftable > So, we can simply get our table using :GetChildren() on the model of the door, then loop through the table and change the CFrame values |
|
|
| Report Abuse |
|
|
lama321
|
  |
| Joined: 18 Dec 2011 |
| Total Posts: 300 |
|
|
| 29 Sep 2014 07:07 PM |
> So the code should look something like this
doorIsOpen = false debounce = true door = game.Workspace.B1.Door:GetChildren() -- Change this to a reference to the door
function onClicked() if debounce == true debounce = false if doorIsOpen == true then for i=1, 20 do for i, v in pairs(door) v.CFrame = v.CFrame + Vector3.new(1, 0, 0) end wait(0.1) end doorIsOpen = false else for i=1, 20 do for i, v in pairs(door) v.CFrame = v.CFrame + Vector3.new(-1, 0, 0) end wait(0.1) end doorIsOpen = true end wait(0.5) debounce = true end end
script.Parent.MouseClick:connect(onClicked) |
|
|
| Report Abuse |
|
|
lama321
|
  |
| Joined: 18 Dec 2011 |
| Total Posts: 300 |
|
|
| 29 Sep 2014 07:11 PM |
> Oh, and i almost forgot
> You can also use 'welds', while i am afraid i can't go through this now as i am quite time poor you can basically grab a welding plugin, weld all the parts in the door to a single master brick > You then just CFrame the master brick and everything else will follow along
> If you don't want to use a welding plugin, there are ways to do it yourself from a script, but i'm afraid you will have to look at the wiki for more info on that |
|
|
| Report Abuse |
|
|
|
| 29 Sep 2014 07:28 PM |
| Im testing it right now, so far no luck, but im tweaking. Trying to see if i can get it to work. |
|
|
| Report Abuse |
|
|