I made a script a while ago that saves the players tools when they leave the game and loads them back when the player enters the game. After I added filtering enabled to my game the script no longer works and for some reason it is breaking my leaderstats save script too. When I buy a weapon from the store, my coins goes down depending on how much I spent on the item. After rejoining the weapon is not saved and my coins are back to the way they were before i bought the item.
01 | local Inventory = { } |
02 |
03 | local function Spawned(Char) |
04 | local Plr = game.Players:GetPlayerFromCharacter(Char) |
05 | for i,v in pairs (Inventory [ Plr ] ) do |
06 | if Plr [ 'Backpack' ] :findFirstChild(v.Name) then |
07 | Plr [ 'Backpack' ] [ v.Name ] :Destroy() |
08 | end |
09 | v.Parent = Plr [ 'Backpack' ] |
10 | end |
11 | Inventory [ Plr ] = { } |
12 | Char:WaitForChild( 'Humanoid' ).Died:connect( function () |
13 | for i,v in pairs ( { Plr [ 'Backpack' ] , Char } ) do |
14 | for ii,vv in pairs (v:GetChildren()) do |
15 | if vv:IsA( 'Tool' ) then |
Also the leaderstats save on their own. So if I earn 20 coins in game they save, but not when I purchase an item.
The main problem with your script is that it's local, and that you are trying to use the PlayerAdded event on a local script.
Also, don't use deprecated functions. (for example ":connect" should be changed to ":Connect".)
Also, here is the fixed script:
01 | local Inventory = { } |
02 |
03 | local function Spawned(Char) |
04 | local player = game.Players:GetPlayerFromCharacter(Char) |
05 | for i,v in pairs (Inventory [ player ] ) do |
06 | if player [ 'Backpack' ] :FindFirstChild-(v.Name) then |
07 | player [ 'Backpack' ] [ v.Name ] :Destroy() |
08 | end |
09 | v.Parent = player [ 'Backpack' ] |
10 | end |
11 | Inventory [ player ] = { } |
12 | Char:WaitForChild( 'Humanoid' ).Died:Connect( function () |
13 | for i,v in pairs ( { player [ 'Backpack' ] , Char } ) do |
14 | for ii,vv in pairs (v:GetChildren()) do |
15 | if vv:IsA( 'Tool' ) then |
! IMPORTANT ! This script must be a server-side script, and NOT a local-script.
Hope this helps.