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

Is there a way to save and load tools?

Asked by 9 years ago

I'm currently looking for a script that can save tools so that when you come back, they are in your inventory. I'm NOT asking for someone to completely make a script for me. I am just trying to get a lead to making tools save.

2 answers

Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
9 years ago
Edited 7 years ago

In order to save tools, we would have to save the names (String) of the tools, instead of the tools (Instance) themselves, because Instances are not yet supported by data stores. A little bit more detail will be provided down below.

Using functions, the saving and loading of data could be simplified written in a much neater manner. The code below assumes that the tools are stored in ReplicatedStorage.

Everytime the PlayerAdded event is fired, the getData function is called with the argument being the newly added player. The returned package from the call to function getData is set as value to variablearmory.

Looping through everything in armory, or the result of the calling of getData, every value is passed as an argument to FindFirstChild, which also loops through storage in this case for any child with the name of the value. The matching tool is cloned and then parented to the player's Backpack.

When the PlayerRemoving event is fired for the local player, a quick loop of his backpack is ran in search for weapons. When found, the names of said weapons are added into the table output.


local dataStore = game:GetService('DataStoreService')
local storage = game:GetService('ReplicatedStorage')
local Weapons = dataStore:GetDataStore('Weapons')
local players = game:GetService('Players')

local updateData = function (player, package)
    local key = (player.userId)

    if type(package) == type({}) then
        Weapons:UpdateAsync(key, function (old)
            return package
        end)
    end
end
local getData = function (player)
    local key = (player.userId)
    return Weapons:GetAsync(key) or {}
end 

players.PlayerAdded:connect(function (player)
    local backpack = player.Backpack
    local armory = getData(player)

    for index, value in ipairs(armory) do
        if storage:FindFirstChild(value) then
            local match = value:Clone()
            match.Parent = backpack
        end
    end
end)


players.PlayerRemoving:connect(function (player)
    local backpack = player.Backpack
    local output = {}

    for index, value in pairs(backpack:GetChildren()) do
        if value:isA('Tool') then
            output[#output + 1] = value.Name
        end
    end
    updateData(player, output)
end)

game.OnClose = function() wait(5) end

Ad
Log in to vote
0
Answered by
iaz3 190
9 years ago

Assuming the tools are all ingame, in every game, you could store a string representation of which tools the player currently has, and if needed some extra values, and save that.

You could then load that and copy the appropriate tools and set values accordingly.

No need to save the actual tools themselves

Answer this question