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

Tool DataStore Not Working Correctly?

Asked by 7 years ago

I'm completely stumped right now. I have a DataStore script but it saves weapons at times but after playing 2 or more times the weapon eventually does not spawn anymore. Can someone please take a look at this script for me.

Also if you know of any other DataStore scripts that may be better I would be more than willing to replace this one with a more stable script.

Thanks in advance.

wait(3)
local RunService = game:GetService("RunService")
local ToolData = game:GetService("DataStoreService"):GetDataStore("ToolData")

local ToolLocation = game.ServerStorage.Tools -- The location where all your tools are saved

function GetToolByName(toolName)
    for _, tool in pairs(ToolLocation:GetChildren()) do
        if tool.Name == toolName then return tool end
    end
end

function IsRegisteredTool(tool)
    for _, playerTool in pairs(ToolLocation:GetChildren()) do
        if playerTool.Name == tool.Name then return true end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    repeat wait() until player.Character
    local dataKey = "tool-data_"..player.UserId
    if ToolData:GetAsync(dataKey) then
        for i, toolName in pairs(ToolData:GetAsync(dataKey)) do
            local newStarterTool = GetToolByName(ToolData:GetAsync(dataKey)[i]):Clone()
            local newTempTool = GetToolByName(ToolData:GetAsync(dataKey)[i]):Clone()
            newStarterTool.Parent = player.StarterGear
            newTempTool.Parent = player.Backpack
        end
        ToolData:SetAsync(dataKey, {})
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local dataKey = "tool-data_"..player.UserId
    local playerTools = {}
    for _, tool in pairs(player.StarterGear:GetChildren()) do
        if tool:IsA("Tool") and IsRegisteredTool(tool) then table.insert(playerTools, 1, tool.Name) end
    end
    if RunService:IsStudio() or #playerTools < 1 then return end
    ToolData:SetAsync(dataKey, playerTools)
end)

Answer this question