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

What on earth went wrong here? [SOLVED]

Asked by
KordGamer 155
10 years ago

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

01open = 0
02Cstore = script.Parent.CFrame
03local player = game.Players.LocalPlayer
04 
05 
06 
07 
08 
09 
10 
11 
12function onClick()
13 
14 
15    if open == 0 then
View all 43 lines...

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!

0
Done that, New error appeared. Workspace.LockedWithKeyDoor.TraditionalDoor.Door.Script:19: attempt to index global 'player' (a nil value) KordGamer 155 — 10y
0
Is this in a LocalScript? dyler3 1510 — 10y
0
No. I guess that's where I went wrong :P KordGamer 155 — 10y

2 answers

Log in to vote
2
Answered by 10 years ago

First, you don't need so much white space in your code, let's fix that first:

01open = 0
02Cstore = script.Parent.CFrame
03local player = game.Players.LocalPlayer
04 
05function onClick()
06    if open == 0 then
07        if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then
08    open = 3
09        script.Parent.Open_Door:Play()
10        for i = 1,16 do
11        wait(0.01)
12        script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0)
13        end
14    open = 1
15    elseif open == 1 then
View all 30 lines...

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:

01open = 0
02Cstore = script.Parent.CFrame
03 
04function onClick(player) --The event gives the player as a parameter, so you can just add a variable to reference that parameter in the parenthesis.
05    if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then
06        if open == 0 then
07            open = 3
08            script.Parent.Open_Door:Play()
09            for i = 1,16 do
10                wait(0.01)
11                script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0)
12            end
13            open = 1
14        elseif open == 1 then
15            open = 3
View all 29 lines...

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.

0
Thanks :D KordGamer 155 — 10y
0
Lol, beat me to it dyler3 1510 — 10y
0
No problem @Kord. Don't worry, dyler, I always get beaten to answering a question, I'll give your answer an upvote too. Spongocardo 1991 — 10y
0
In the script, you needed to keep that end. You missed one of the "if" statements towards the start of the function I think. dyler3 1510 — 10y
View all comments (3 more)
0
Lol, thanks tho. It's frustrating sometimes xD . I upvoted as well. dyler3 1510 — 10y
0
Yeah, the code was a bit hard to look at and I had to rearrange a few pieces to make it work. That code should work now. And no problem. Spongocardo 1991 — 10y
1
Dude. Your script that you made is art. I will call it, the biggest space universe. Since your question was WHAT ON EARTH IS WRONG WITH IT, then it is perfect to call it Big Space. RobotChitti 167 — 10y
Ad
Log in to vote
3
Answered by
dyler3 1510 Moderation Voter
10 years ago

I think I figured out what's wrong. You had a few problems here:

01open = 0
02Cstore = script.Parent.CFrame
03 
04function onClick(player)
05    if open == 0 then
06        if player.Backpack:FindFirstChild(script.Parent.ItemName.Value) then
07            open = 3
08            script.Parent.Open_Door:Play()
09            for i = 1,16 do
10                wait(0.01)
11                script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0.25, 0, 0) * CFrame.fromEulerAnglesXYZ(0, math.rad(-5.625), 0)
12            end
13            open = 1
14        elseif open == 1 then
15            open = 3
View all 29 lines...

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

Answer this question