I have a variable wich is player:WaitForChild(“Backpack”) and when i print its parent, it says nil.
I simply don’t know why and i cannot figure it out.
Here is part of the script.
local GunModule = {}
--Folders & Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GunsFolder = ReplicatedStorage.Guns
--Player stuff
local Player = game.Players.LocalPlayer
local PlayerBackpack = Player:WaitForChild("Backpack")
function GunModule.NewGun()
local NewGun = {}
setmetatable(NewGun, {__index = NewGun})
local NewGunTool = GunsFolder.StarterGun:Clone()
NewGunTool.Parent = PlayerBackpack
NewGun.GunTool = NewGunTool
NewGun.FireWait = .1
NewGun.InitialDamage = 10
NewGun.Range = 100
NewGun.DamageLoseRatio = 0.5
NewGun.GunType = "Auto"
print(PlayerBackpack:GetChildren())
end
Is this a Script or a LocalScript? LocalPlayer can only be referenced in LocalScripts and will return nil when you try to reference it in a server-sided script, which would explain your issue here.
If you want to get the backpack after the player dies you need to get the new one in the player (either change the variable when the player dies or always get the new one when the code needs the backpack).
I believe the problem is that you don’t have connections to clean up your new gun object when the humanoid dies. If your gun object is destroyed then it won’t try to access the destroyed Backpack.
This also probably prevents a memory leak where the Backpack is never garbage collected because your new gun object references it.
So i kinda of forgot about this post, but i fixed everything. Turns out, after the backpack loads the first time, it loads again (why?) and that first backpack variable becomes nil. i fixed it by making the variable inside the GunModule.NewGun() function. Thank you for anyone who helped.