Inventory stacking question (CHECK REPLIES FOR FULL CONTEXT)

local RS = game:GetService("RunService")

RS.Stepped:Connect(function()
	local tool = script.Parent.Parent
	tool.ToolTip = tostring(script.Parent.Value).." remaining..."
end)

Why does this provide an error on line 4 “attempted to index nil with parent”

image

3 Likes

You could probably try making variables instead of calling script.Parent in the function.

local stacks = script.Parent
local tool = stacks.Parent

local RS = game:GetService("RunService")

RS.Stepped:Connect(function()
	tool.ToolTip = tostring(stacks.Value).." remaining..."
end)

And if you don’t mind, can you send a picture of the output box after running this?

3 Likes

Same thing but one error as opposed to an error every step.

I’m going to change the topic of this post to a larger issue I seem to be encountering now.

I’m creating and inventory tool stacking system and the way I’m going about it creating a stack intvalue in tools that can be stacked and checking everytime something is added to the players backback. If the tool added is the same as a tool already existing either equipped or in the players bag then it will destroy the newly added tool and add to the stacks of the already existing tool. The script that was malfunctioning at first was a simple tracker to let the player check how many instances of a tool they had left however now I seem to be encountering issues with the larger system. I’ll send photos in just a second.

1 Like

Stacking script:

image

1 Like

change the plr variable to game:GetService("Players"):WaitForChild(chr.Name)

and on the off spawn stacks to for i,v in pairs(plr:WaitForChild("Backpack")) do

It seems that your Script’s RunContext set to Server/Client. It will also run in the StarterCharacterScripts container when it’s not supposed to. That means the chr variable in your StarterCharacterScripts script is not the actual character.

2 Likes

Yes that’s true and it can be quite annoying to make it so these scripts only run after being placed in the character however not difficult. Would also like to add that depending on your inventory system (as I am using a custom one) you may have to incorporate a wait() so the custom inventory script has time to read the adding and removing of children from the backpack/character.

That’s just bad practice. Anyways,

  1. RunContext is meant to unify server/client into a more organized pattern, or to run scripts on containers that normally wouldn’t run on.
  2. Use this pattern instead
local function onToolAdded(tool: Instance): ()
    if tool.ClassName == "Tool" then
        -- do stuff if haven't hooked already
    end
end

backpack.ChildAdded:Connect(onToolAdded)
for _, child: Instance in backpack:GetChildren() do
    task.spawn(onToolAdded, child)
end

I don’t understand how this works, does this not still contain the issue of having to first define whether or not the script is located under i.e a plr/chr rather than i.e startercharcterscripts, replictaed storage etc. To define backpack you have to define player and to define player you still need to check if the script is located somewhere within player no? (Would also like to clarify I don’t NEED this to run on the server but it is easier than using remote events to transfer the value of stacks and location of tools to the server every update. I need stacks serversided for other scripts to recognize and utilize the value.

I still don’t really have a clear idea of what you are trying to say. And your pre-edit, you said that it works but just throws an error, and that is because of the RunContext behavior.

Let’s say you have a LocalScript inside StarterCharacterScripts with the RunContext set to Legacy:

  1. You have a LocalScript inside game.StarterPlayer.StarterCharacterScripts that won’t run because it is not a valid container that the script can run on.
  2. You have a LocalScript inside game.Players.LocalPlayer.Character whenever you spawn that will run because it is a valid container that the script can run on.

This is the intended behavior because if the LocalScript inside game.StarterPlayer.StarterCharacterScripts runs (with RunContext set to Client), and you have a variable that is something like:

local character = script.Parent

that variable will be StarterCharacterScripts instead of the intended character, and when you try to get that player with:

local player = game:GetService("Players"):GetPlayerFromCharacter(character)

that variable will return nil, etc. etc…

In either RunContext cases, the script will still clone to the Character upon spawning, which will work as expected.

Yes I apologize for the edits they make this confusing. The reason an error was being thrown was because the delay between a tool being cloned and then running the tracker script to update the tooltip in regards to the number of stacks was so little the tool hadn’t been parented to the player’s backpack yet, after adding a slight delay so the tracking would only run after the tools parent had been correctly set the error went away. (This could be slightly incorrect as I have a lot of scripts interacting with each other and I lose the intricacies of them in my head but it is the correct general idea.).
It seems I may’ve also been mistaken with the use of runcontext, I use studio very sporadically and there are often large changes in what’s deprecated and in use when I get on again. I seem to have mistakenly assumed the runcontext was a new way of creating a server/client sided script and that LocalScript had been deprecated or was in the process of being.

This is also because of the RunContext being set to Server/Client. Scripts with the Legacy RunContext do not run in the ReplicatedStorage in the first place.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.