I was programming and I came across an error I have never seen before. I will show both the code and the error below.
This is the code
open = 0 Cstore = script.Parent.CFrame local player = game.Players.LocalPlayer function onClick() if open == 0 then if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then open = 3 script.Parent.Open_Door:Play() for i = 1,16 do wait(0.01) script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0) end open = 1 elseif open == 1 then open = 3 for i = 1,16 do wait(0.01) script.Parent.Close_Door:Play() script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-0.25, 0, 0.028) * CFrame.fromEulerAnglesXYZ(0, math.rad(5.625), 0) end script.Parent.CFrame = Cstore open = 0 else print("Player doesn't own key.") end end end script.Parent.ClickDetector.MouseClick:connect(onClick)
And this is the error:
18:07:09.974 - Workspace.LockedWithKeyDoor.TraditionalDoor.Door.Script:19: attempt to index upvalue 'player' (a nil value)
18:07:09.976 - Stack Begin
18:07:09.977 - Script 'Workspace.LockedWithKeyDoor.TraditionalDoor.Door.Script', Line 19
18:07:09.977 - Stack End
.
I have no clue what went wrong. Any help is appreciated, Thanks!
First, you don't need so much white space in your code, let's fix that first:
open = 0 Cstore = script.Parent.CFrame local player = game.Players.LocalPlayer function onClick() if open == 0 then if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then open = 3 script.Parent.Open_Door:Play() for i = 1,16 do wait(0.01) script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0) end open = 1 elseif open == 1 then open = 3 for i = 1,16 do wait(0.01) script.Parent.Close_Door:Play() script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-0.25, 0, 0.028) * CFrame.fromEulerAnglesXYZ(0, math.rad(5.625), 0) end script.Parent.CFrame = Cstore open = 0 else print("Player doesn't own key.") end end end script.Parent.ClickDetector.MouseClick:connect(onClick)
Your code is much easier to look at now.
Now, as for your problem, I'm going to assume you're using a LocalScript. A LocalScript will not work outside of a child of the player, therefore your script will not work.
If your script is a Script, you can't reference the LocalPlayer in a server script, which is why the player is nil.
The MouseClick event already returns the player that clicked the part, so all you need to do is reference the player as a parameter in your function, like this:
open = 0 Cstore = script.Parent.CFrame function onClick(player) --The event gives the player as a parameter, so you can just add a variable to reference that parameter in the parenthesis. if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then if open == 0 then open = 3 script.Parent.Open_Door:Play() for i = 1,16 do wait(0.01) script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0) end open = 1 elseif open == 1 then open = 3 for i = 1,16 do wait(0.01) script.Parent.Close_Door:Play() script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-0.25, 0, 0.028) * CFrame.fromEulerAnglesXYZ(0, math.rad(5.625), 0) end script.Parent.CFrame = Cstore open = 0 end else print("Player doesn't own key.") end end script.Parent.ClickDetector.MouseClick:connect(onClick)
See how I rearranged your code so it checks if the player has the key first? This is because the player would not need the key if the open variable was one of the other 2 options.
I hope my answer helped you. If it did, be sure to accept it.
I think I figured out what's wrong. You had a few problems here:
open = 0 Cstore = script.Parent.CFrame function onClick(player) if open == 0 then if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then open = 3 script.Parent.Open_Door:Play() for i = 1,16 do wait(0.01) script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0) end open = 1 elseif open == 1 then open = 3 for i = 1,16 do wait(0.01) script.Parent.Close_Door:Play() script.Parent.CFrame = script.Parent.CFrame * CFrame.new(-0.25, 0, 0.028) * CFrame.fromEulerAnglesXYZ(0, math.rad(5.625), 0) end script.Parent.CFrame = Cstore open = 0 else print("Player doesn't own key.") end end end script.Parent.ClickDetector.MouseClick:connect(onClick)
You tried to define the player as LocalPlayer, which can't be done unless the script is a LocalScript, and it's inside the Player. To do this with a ClickDetector, define it as an argument in the function. In your script, I just put it as "player", and kept the rest of the script the same.
Also, try to keep your script a bit cleaner looking like it looks now. (It's easier for others to edit and help you this way)
Basically, that was your only problem, but it should be fixed now. Anyways, if you have any further problems, or questions, please leave a comment below. Hope I helped :P