Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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 comments

0

Try using a remotefunction

2 answers

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! :)

9 comments

0

^

0

ehm and if this will really be the reason then Write Vector3.new(942, 3.24, -847.38)

0

good spot!. changed it. i re-wrote this on my phone. Must stop doing that lol

0

So basically, I can't mention property's in variables? Correct?

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.

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?

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!

0

Also changed my answer with the new updated code and added some comments explaining what i did.

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.