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

How would i make it so it doesnt error when the Value doesnt exists?

so what i mean by that is im checking to see if something exists, and if it doesnt text says its locked. but the text cant show up because it errors saying the Value doesnt exists when it isnt supposed to, is there a better way to check?

local key1 = workspace.Key1

script.Parent.ClickDetector.MouseClick:connect(function(click)
	local moving = false
	if moving == false then
	moving = true
	if click.Backpack.Key1 then
		key1.Sound:Play()
		key1.Sound:Destroy()
		key1.ClickDetector:Destroy()
		game.Players.LocalPlayer.Backpack.Key1:Destroy()
		local num = 0
		repeat 
			num = num + 1
			key1.CFrame = key1.CFrame * CFrame.new(0, 0, 0.35) * CFrame.fromEulerAnglesXYZ(0, 0.1, 0)
			wait(0.01)
			until num > 17
	end
	else
		script.Parent.Text = "Hmm... Its locked..."
		script.Parent.Visible = true
		for i= 1,10 do
		wait()
		script.Parent.TextTransparency = 1-(i*0.1)
	end		
	wait(2)
	for i= 1,10 do
		wait()
		script.Parent.TextTransparency = (i*0.1)
		end
	end
end)

Error: > 22:30:45.418 - Key1 is not a valid member of Backpack

2 answers

On line 7, instead of:

if click.Backpack.Key1 then

Make it:

if click.Backpack:findFirstChild("Key1") then

Final Script

local key1 = workspace.Key1

script.Parent.ClickDetector.MouseClick:connect(function(click)
	local clicker = click -- storing the player that clicked
	local moving = false
	if moving == false then
		moving = true
	if clicker.Backpack:findFirstChild("Key") then
		print("working")
		key1.Sound:Play()
		key1.Sound:Destroy()
		key1.ClickDetector:Destroy()
		clicker.Backpack.Key1:Destroy()
		local num = 0
		repeat 
		num = num + 1
		key1.CFrame = key1.CFrame * CFrame.new(0, 0, 0.35) * CFrame.fromEulerAnglesXYZ(0, 0.1, 0)
		wait(0.01)
		until num > 17
	end
	else
		script.Parent.Text = "Hmm... Its locked..."
		script.Parent.Visible = true
		for i= 1,10 do
			wait()
			script.Parent.TextTransparency = 1-(i*0.1)
		end
	end     
	wait(2)
	for i= 1,10 do
		wait()
		script.Parent.TextTransparency = (i*0.1)
	end
end)

I tested it and it worked perfectly fine for me. So you might be doing something wrong.

Hope this helps!

5 comments

0

It still gives me the error

0

Can you tell me which line exactly errors?

0

no it just says that Key1 doesnt exist

0

but its not supposed to exist untill you get the key

0

It should look something like this, it always gives you the script name and its parenting and the line number:   00:18:14.034 - Key1 is not a valid member of Backpack 00:18:14.035 - Script 'ServerScriptService.Script', Line 1  --- This line right here

The problem here is that if Key1 doesn't exist, asking the script to look for Key1 will be impossible! There's a simple way to fix this.

if click.Backpack:FindFirstChild("Key1") ~= nil then

FindFirstChild looks for the child with the name in quotations. If it doesn't exist, it returns nil. So if it doesn't return nil, the script continues!

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