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

Object Value is recognized as nil value in local script and not server script for Inventory System?

Asked by 4 years ago

I'm currently trying to make a backpack/inventory system with keypress and clicking but the server script doesn't work with keypress and the local script has the following error:

Local Script Error:

Players.WyattagumsBackUp.PlayerGui.BackpackGUI.Background.a.equipbutton.LocalScript:24: attempt to index upvalue 'itm' (a nil value)

Local Script:

local lab = script.Parent.Parent.label
local num = script.Parent.Parent.number
local but = script.Parent
local pak = script.Parent.Parent.Parent.Backpack
local itm = script.Parent.Parent.item.Value
local plr = game.Players.LocalPlayer
local chr = plr.Character
local eqp = script.Parent.Parent.equipped.Value

function onClicked()
    if eqp == false then
        itm.Parent = chr
        eqp = true
            elseif eqp == true then
                itm.Parent = pak
                eqp = false
    end 
end
script.Parent.MouseButton1Down:connect(onClicked)

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.One then
        if eqp == false then
            itm.Parent = chr
            eqp = true
                elseif eqp == true then
                    itm.Parent = pak
                    eqp = false

        end 
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

The server script works perfectly with the clicking but not the keypress, whilst the local script sends out the said error...

Server Script:

local lab = script.Parent.Parent.label
local num = script.Parent.Parent.number
local but = script.Parent
local pak = script.Parent.Parent.Parent.Backpack
local itm = script.Parent.Parent.item.Value
local plr = script.Parent.Parent.Parent.Parent.Parent.Parent
local chr = plr.Character
local eqp = script.Parent.Parent.equipped.Value

function onClicked()
    if eqp == false then
        itm.Parent = chr
        eqp = true
            elseif eqp == true then
                itm.Parent = pak
                eqp = false
    end 
end
script.Parent.MouseButton1Down:connect(onClicked)

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.One then
        if eqp == false then
            itm.Parent = chr
            eqp = true
                elseif eqp == true then
                    itm.Parent = pak
                    eqp = false

        end 
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Both scripts are relatively the same, except for the one change which is the "local plr"

Here is an image of the system paths: https://imgur.com/MoSPTkR

If you would require further explanation/information then please do respond to this post on your concerns!

1 answer

Log in to vote
0
Answered by
Maxis_s 97
4 years ago
Edited 4 years ago

You cannot use a variable directly on a value. Each time, you have to use Instance.Value, while instance is a value, in your case, an object value.

EDIT: Added new script. If this doesn't work, comment.

local lab = script.Parent.Parent.label
local num = script.Parent.Parent.number
local but = script.Parent
local pak = script.Parent.Parent.Parent.Backpack
local itm = script.Parent.Parent.item
local plr = game.Players.LocalPlayer
local chr = plr.Character
local eqp = script.Parent.Parent.equipped

function onClicked()
    if eqp.Value == false then
        itm.Parent = chr
        eqp.Value = true
            elseif eqp.Value == true then
                itm.Parent = pak
                eqp.Value = false
    end 
end
script.Parent.MouseButton1Down:connect(onClicked)

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.One then
        if eqp.Value == false then
            itm.Parent = chr
            eqp.Value = true
                elseif eqp.Value == true then
                    itm.Parent = pak
                    eqp.Value = false

        end 
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Hope this helps!

-Maxis_s

0
How would I directly implement that into my script? WyattagumsBackUp 5 — 4y
0
Lemme add it. Maxis_s 97 — 4y
0
k WyattagumsBackUp 5 — 4y
0
Edited. Maxis_s 97 — 4y
View all comments (4 more)
0
oh okay~ I'm so silly for making such an insultingly benign mistake! WyattagumsBackUp 5 — 4y
0
This is a mistake I've done, but hey, it's a sign of learning! Maxis_s 97 — 4y
0
Dully note that we forgot to add ".Value" to the itm, so itm.Parent would be itm.Value.Parent WyattagumsBackUp 5 — 4y
0
itm.Value.Parent would return itm. Maxis_s 97 — 4y
Ad

Answer this question