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

I get error: "Attempt to call a nil value" How do i fix?

Asked by 7 years ago
Edited 7 years ago

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.

0
it will attempt to do all of the functions at once so try to make it into one, also you have to define the functions before calling them viltsuviltsu 4 — 7y
0
Ok, Thanks. bestrindberg 2 — 7y
0
But wait... When i click one thing only one function runs. Not all. bestrindberg 2 — 7y
0
Tried it, Didn't work. bestrindberg 2 — 7y
View all comments (2 more)
0
Or you didn't define correctly the location of the doors that's why it appears as nil (non existant) SulaymanArafat 230 — 7y
0
Can you share your hierarchy? RockerCaleb1234 282 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

0
You see thats why Variables are very important because it makes things really short saSlol2436 716 — 7y
0
I understand what you mean. Im going to try it right away! p.s you're really good at explaining! bestrindberg 2 — 7y
0
It worked! Thank you! :D bestrindberg 2 — 7y
0
Your welcome saSlol2436 716 — 7y
Ad

Answer this question