Im trying to make a kitchen counter with doors that can open, However when i test the game and click the door it says: "Attempt to call a nil value" Why does this happen, And how can i fix it? Code:
script.Parent.Door1.ClickDetector.MouseClick:connect(Door1Open) script.Parent.Door2.ClickDetector.MouseClick:connect(Door2Open) script.Parent.Door1Open.ClickDetector.MouseClick:connect(Door1Close) script.Parent.Door2Open.ClickDetector.MouseClick:connect(Door2Close) function Door1Open() script.Parent.Door1.Transparency = 1 script.Parent.Door1Open.Transparency = 0 end function Door2Open() script.Parent.Door2.Transparency = 1 script.Parent.Door2Open.Transparency = 0 end function Door1Close() script.Parent.Door1.Transparency = 0 script.Parent.Door1Open.Transparency = 1 end function Door2Close() script.Parent.Door2.Transparency = 0 script.Parent.Door2Open.Transparency = 1 end
Also, Im quite new to this programming thing. So try to simplify answers as much as possible. Thank you.
What I'm thinking your hiearchy is like Doors -->> Script, Door-->>ClickDetector, Door2-->ClickDetector Since you are new to scripting here is an important thing What you did originally was that you called the function before even saying what the function is so thats why its saying "error attempt to call a nil value" It's like somebody saying "Do your homework" even though the teacher didn't assign homework, same thing here with functions As you can see I changed connect to Connect Well connect is depricated If something is depricated, then you can't use something recent with it Thats why I reccommend using Connect instead of connect I added a few variables to make it more easier to read Plus I don't like using transparency I used for loops to make a door animation
local fullModel = script.Parent local door1 = fullModel.Door1 local click1 = door1.ClickDetector local door2 = fullModel.Door2 local click2 = fullModel.ClickDetector function Door1Open() for openDoor = 0,90.0.1 do door1.Orientation = Vector3.new(0,openDoor,0) -- Simple animation wait() end end function Door2Open() for openDoor2 = 0,90.0.1 do door1.Orientation = Vector3.new(0,openDoor2,0) -- Simple animation wait() end end function Door1Close() for closeDoor = 90,0,-0.1 do 90 + (-0.1) is the same thing as 90 - 0.1 door1.Orientation = Vector3.new(0,closeDoor,0) -- Simple animation end end function Door2Close() for closeDoor2 = 90,0,-0.1 do 90 + (-0.1) is the same thing as 90 - 0.1 door2.Orientation = Vector3.new(0,closeDoor,0) -- Simple animation end end click1.MouseClick:Connect(Door1Open) click2.MouseClick:Connect(Door2Open) click1.MouseClick:Connect(Door1Close) click2.MouseClick:Connect(Door2Close)
If you have no knowledge about loops then I reccommend you to learn it right away Loops are very important Anyway If this answer worked then, Please submit it