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

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

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

✓ Accepted answer

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

8 comments

0

How would I directly implement that into my script?

0

Lemme add it.

0

k

0

Edited.

0

oh okay~ I'm so silly for making such an insultingly benign mistake!

0

This is a mistake I've done, but hey, it's a sign of learning!

0

Dully note that we forgot to add ".Value" to the itm, so itm.Parent would be itm.Value.Parent

0

itm.Value.Parent would return itm.

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