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

What on earth went wrong here? [SOLVED]

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!

3 comments

0

Done that, New error appeared. Workspace.LockedWithKeyDoor.TraditionalDoor.Door.Script:19: attempt to index global 'player' (a nil value)

0

Is this in a LocalScript?

0

No. I guess that's where I went wrong :P

2 answers

✓ Accepted answer

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.

7 comments

0

Thanks :D

0

Lol, beat me to it

0

No problem @Kord. Don't worry, dyler, I always get beaten to answering a question, I'll give your answer an upvote too.

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.

0

Lol, thanks tho. It's frustrating sometimes xD . I upvoted as well.

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.

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.

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

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