Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Saving Tools Script not working with Filtering Enabled?

Asked by 5 years ago

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.

local Inventory = {}

local function Spawned(Char)
    local Plr = game.Players:GetPlayerFromCharacter(Char)
    for i,v in pairs(Inventory[Plr]) do
        if Plr['Backpack']:findFirstChild(v.Name) then
            Plr['Backpack'][v.Name]:Destroy()
        end
        v.Parent = Plr['Backpack']  
    end
    Inventory[Plr] = {}
    Char:WaitForChild('Humanoid').Died:connect(function()
        for i,v in pairs({Plr['Backpack'], Char}) do
            for ii,vv in pairs(v:GetChildren()) do
                if vv:IsA('Tool') then
                    table.insert(Inventory[Plr], vv:Clone())
                    vv:Destroy()
                end
            end
        end
    end)
end

game.Players.PlayerAdded:connect(function(Plr)
    Inventory[Plr] = {}
    local Char = Plr.Character or Plr.CharacterAdded:wait()
    Spawned(Char)
    Plr.CharacterAdded:connect(Spawned)
end)

Also the leaderstats save on their own. So if I earn 20 coins in game they save, but not when I purchase an item.

0
Also yes it is in a Local Script. tawk1215 47 — 5y
0
PlayerAdded is server side only, connect, :wait, :findFirstChild are deprecated, use Connect, Wait, FindFirstChild User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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:

local Inventory = {}

local function Spawned(Char)
    local player= game.Players:GetPlayerFromCharacter(Char)
    for i,v in pairs(Inventory[player]) do
        if player['Backpack']:FindFirstChild-(v.Name) then
            player['Backpack'][v.Name]:Destroy()
        end
        v.Parent = player['Backpack']  
    end
    Inventory[player] = {}
    Char:WaitForChild('Humanoid').Died:Connect(function()
        for i,v in pairs({player['Backpack'], Char}) do
            for ii,vv in pairs(v:GetChildren()) do
                if vv:IsA('Tool') then
                    table.insert(Inventory[player], vv:Clone())
                    vv:Destroy()
                end
            end
        end
    end)
end

game.Players.PlayerAdded:Connect(function(player)
    Inventory[player] = {}
    local Char = player["Character"]
    Spawned(Char)
    Player.CharacterAdded:Connect(Spawned)
end)

! IMPORTANT ! This script must be a server-side script, and NOT a local-script.

Hope this helps.

Ad

Answer this question