Hello, I’ve been working on a custom hotbar script for a bit now, and I ran into this issue. The local script used for this is located in StarterPlayerScripts.
I do understand that PlayerScripts don’t reset on character death, however shouldn’t this still print even after the character loads in again?
I’ve tried plenty of methods like disconnecting the function, making an RBXScriptConnection variable, however none of it worked.
If I have to insert the script into StarterCharacterScripts, I will, however I prefer for the script to be parented in StarterPlayerScripts. (Also I am a relatively new scripter )
Edit: The reply I tagged as a solution is not an in-script solution you could say, and is more of a workaround for the issue. In short; put the local script in StarterCharacterScripts when working with the players backpack.
local player = game.Players.LocalPlayer
local backpack = player.Backpack
player.CharacterAdded:Connect(function()
local plr = game.Players.LocalPlayer
backpack = plr.Backpack
end)
----make new script to disable and enable the script after the player reset
local thescript = --script
local player = game.Players.LocalPlayer
player.CharacterAdded:Connect(function()
player = game.Players.LocalPlayer
thescript.Disabled = true
thescript.Disabled = false
end)
function printChecker()
print("Tool has been added to pack")
end
backpack.ChildAdded:Connect(printChecker)
game.Players.LocalPlayer.CharacterAdded:Connect(function()
for _, v in backpack:GetChildren() do
printChecker(v)
end
end
I’m not looking to print the tools themselves or any number, I put the print statement there to check if the function would work. Unfortunately, it did not. What I want to do is to ensure that the function can be reused after the player respawns.
I don’t believe so as it works perfectly fine when placed inside of StarterCharacterScripts, also that would be the whole script just checking if a Child was added to the Backpack
Tried replicating this problem and couldn’t due to the fact that Backpack.ChildAdded:Connect() didn’t work despite my character just spawning in and not dying yet. Howver, as you stated, it breaks for you when the character dies, which means it works for you until the character dies, which is opposite to my problem as mine just doesn’t even work. So I was just wondering what you did to make yours work.
And yes, your code should run even if the character dies. So again, this could be a bug.
I tried making a separate script and wrote basically the same thing, and it does not work at all. It’s all the same with it being parented to StarterPlayerScripts, wrote the same functions and everything. It’s basically a 1:1 copy with the other functions and unimportant variables out of the way. Yet, it still does not work even after resetting the character.
Edit: I don’t want to say this is a bug in Roblox studio, as I feel that would be jumping to conclusions, however I’ve worked on this for roughly 14 hours or so, and still can’t figure this out.
Moved the script into starter character scripts and it works as intended. When working with the players Backpack, I believe it should be done in StarterCharacterScripts as the backpack is reset/destroyed and added back to the player. The script I was using in StarterPlayerScripts did not reset I would think. I am still relatively new at this, so take this hypothesis with a grain of salt: I think the variable I called for the backpack local backpack = plr.Backpack was not going to work after the player respawned due to the backpack being a reference to the old backpack before the player’s character reset.
In addition, it is REQUIRED to have a task.wait, or :Wait() method before calling a variable for the backpack in a local script parented in StarterPlayerScripts as for some reason the script, being able to print the backpacks parent correctly through print(backpack.Parent) , still was not able to access the backpacks children correctly. I hope this helps anyone who spent hours on the same issue I did, or maybe this helped someone who was about to, either way please keep these things in mind when working with the players backpack.
Disconnecting and reconnecting the reference would have been my first solution if you hadn’t phrased it like this lol.
local conn
function printChecker()
print("Tool has been added to pack")
end
conn = game.Players.LocalPlayer.Backpack.ChildAdded:Connect(printChecker)
game.Players.LocalPlayer.CharacterAdded:Connect(function()
conn:Disconnect()
conn = game.Players.LocalPlayer.Backpack.ChildAdded:Connect(printChecker)
end)