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

Why is this script for opening and closing a door not working?

Asked by 3 years ago
local doororientation = script.Parent.Orientation 

local doorposition = script.Parent.Position

function door ()
    doororientation = Vector3(0, 90, 0)
    doorposition = Vector3(942, 3.24, -847.38)
    wait(0.7)
    doororientation =  Vector3(0, 0, 0)
    doorposition = Vector3(940, 3.24, -847.38)
end

script.Parent.ClickDetector.MouseClick:Connect(door)


I'm a beginner at coding, as in I started a few days ago, so I was seeing if I could make a better version of my disappearing door. I was messing around, and I assume I did this script correctly, but I obviously did not. I assumed sense

script.Parent.Orientation = Vector3(0, 90, 0)

worked, I could use it inside of a function, with a variable, but as a beginner, I was sadly wrong. :(

Could anyone explain what I did wrong?

2 answers

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago
Edited 3 years ago

The reason why your door isn't moving is that you're talking to the reference of your door. When you reference the property of an object at game start, it simply copies it to memory. so when you change that property, the original doesn't see the change. To fix this, i would change your code to this..

Try this:

local door = script.Parent -- this would make a reference to the object that can be changed later!.
-- old
-- function door ()-- this needs to be changed
-- to something like this
function Door()
    --// make the necessary changes from our reference object aka door
    door.Orientation = Vector3.new(0, 90, 0)
    door.Position = Vector3.new(942, 3.24, -847.38)
    wait(0.7)
    door.Orientation =  Vector3.new(0, 0, 0)
    door.Position = Vector3.new(940, 3.24, -847.38)
end



-- wat ever you name your function just change this bit to call it example:
-- old
-- script.Parent.ClickDetector.MouseClick:Connect(door)
-- new
script.Parent.ClickDetector.MouseClick:Connect(Door) -- same name as above, including capitalization of the first letter etc...

Hope this helps! :)

0
^ KingDomas 153 — 3y
0
ehm and if this will really be the reason then Write Vector3.new(942, 3.24, -847.38) DefinitelyNotTronix 19 — 3y
0
good spot!. changed it. i re-wrote this on my phone. Must stop doing that lol TGazza 1336 — 3y
0
So basically, I can't mention property's in variables? Correct? SmockyBocky 5 — 3y
View all comments (5 more)
0
kinda correct it depends on the object your referencing. but its good practice to reference the object first then when your script runs change its properties from that object reference. Just like what i put here. TGazza 1336 — 3y
0
The script still does not work, the output error is, "Workspace.door2.0.door2.0script:5: attempt to index function with 'Orientation' " what does this mean? SmockyBocky 5 — 3y
0
Change your function to something different than your door object. That error is referencing both your function AND your door object. In Practice this would be good in the real world but Computers are stupid. They cant tell the difference between the two types. and error out. So change function door() to something like function Door() also dont forget to change your event call! TGazza 1336 — 3y
0
Also changed my answer with the new updated code and added some comments explaining what i did. TGazza 1336 — 3y
0
Basically, that Error is looking for a property of your function named Orientation in which we know it doesn't have. But the door object does!. In Lua function names prioritise over objects or variables, so the computer looks for the properties in that function and when it doesn't find them it errors out. TGazza 1336 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You tried using only with 'Vector3'.

It is supposed to be Vector3.new which returns a Vector3 value by using the 3 values (x,y,z).

Rewrite your code like this:

local doororientation = script.Parent.Orientation 

local doorposition = script.Parent.Position

function door ()
    doororientation = Vector3.new(0, 90, 0)
    doorposition = Vector3.new(942, 3.24, -847.38)
    wait(0.7)
    doororientation =  Vector3.new(0, 0, 0)
    doorposition = Vector3.new(940, 3.24, -847.38)
end

script.Parent.ClickDetector.MouseClick:Connect(door)

I kind of just released something but it is already answered by the first answer made to this question.

Answer this question