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

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

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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!

0
It still gives me the error bubbaman73 143 — 9y
0
Can you tell me which line exactly errors? AbsoluteAxiom 175 — 9y
0
no it just says that Key1 doesnt exist bubbaman73 143 — 9y
0
but its not supposed to exist untill you get the key bubbaman73 143 — 9y
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 AbsoluteAxiom 175 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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!

Answer this question