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

How do I save tools with Filter Enabled?

Asked by 5 years ago

I've tried at least ten Freemodel scripts to save tools they just dont seem to work at my place.. Does anyone have a clue how that works?

1
Don't use free models, learn to script yourself instead Amiaa16 3227 — 5y

1 answer

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

Firstly, make sure that you place each tool inside a Folder named "Tools", the folder should be placed into ServerStorage

Local Script (should be placed inside every tool)

local Remote = game:GetService("ReplicatedStorage"):WaitForChild("ToolEquip")
local Tool = script.Parent

Tool.Equipped:Connect(function()
    Remote:FireServer(Tool.Name)
end)

Server Script (placed in ServerScriptService)

local DSS = game:GetService("DataStoreService")
local Storage = DSS:GetDataStore("Tools")

local Remote = Instance.new("RemoteEvent")
Remote.Name = "ToolEquip"
Remote.Parent = game:GetService("ReplicatedStorage")

game:GetService("Players").PlayerAdded:Connect(function(Player)
    local folder = Instance.new("Folder")
    folder.Name = "SavedTools"
    folder.Parent = Player

    for a, b in pairs(game:GetService("ServerStorage"):WaitForChild("Tools"):GetChildren()) do
        if b:IsA("Tool") then
            local bool = Instance.new("BoolValue")
            bool.Name = b.Name
            bool.Value = false
            bool.Parent = folder
        end
    end

    local success, result = pcall(function()
        Storage:GetAsync(Player.UserId)
    end)

    if not success then
        warn(result)
    else
        local LoadArray = Storage:GetAsync(Player.UserId)
        if LoadArray ~= nil then
            for c, d in pairs(folder:GetChildren()) do
                local find = game:GetService("ServerStorage"):WaitForChild("Tools"):FindFirstChild(d.Name)
                if find ~= nil then
                    d.Value = LoadArray[c]
                end
            end
        end
    end
end)

Remote.OnServerEvent:Connect(function(Player, Tool)
    Player:WaitForChild("SavedTools"):WaitForChild(Tool).Value = true
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player)
    local SaveArray = {}
    for e, f in pairs(Player:WaitForChild("SavedTools"):GetChildren()) do
        local num = #SaveArray + 1
        table.insert(SaveArray, num, f.Value)
    end
    Storage:SetAsync(Player.UserId, SaveArray)
end)

You need to use a Remote Event to communicate between the Server and the Client, for this example, if the player equips a tool, then they will have their value sent to the server to be saved when they leave.

Each tool is given a value when the player enters the game, and then saved back into a table when they leave the game.

We use a pcall to make sure no errors are thrown by the script, preventing its further functionality (since API Services should be disabled, you cannot test this in studio).

Ad

Answer this question