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

datastore script not working (loading data on join)?

Asked by 3 years ago

Hi, just wondering how I could fix this Datastore script?

local DataStoreService = game:GetService("DataStoreService")

local ToolsDataStore = DataStoreService:GetDataStore("ToolsDataStore")


local function saveData(player)

    if player:FindFirstChild("OwnedTools") then
        local tools = {}

        for i, v in pairs(player.OwnedTools:GetChildren()) do
            table.insert(tools,v.Name)
        end

        local success, errorMessage = pcall(function()
            ToolsDataStore:SetAsync(player.UserId.."-tools",tools)
        end)

        if success then
            print("Data saved")
        else
            print("Error: "..errorMessage)
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local data = nil

    wait(1)


    local success, errorMessage = pcall(function()
        data = ToolsDataStore:GetAsync(player.UserId.."-tools")
    end)

    if success then

        print("Got past first line (success).")

        for i, v in pairs(data) do --This is the part not working.

            print("Got past second line (success).")

            local Tool = game.ReplicatedStorage.WeaponsFolder:FindFirstChild(v)
            local WeaponsFolder = player:FindFirstChild("WeaponsFolder")
            local ToolClone = Tool:Clone()
            ToolClone.Parent = WeaponsFolder

            print("Got past last line (success).")

    end
else
    print("Error: "..errorMessage)
end

end)


game.Players.PlayerRemoving:Connect(function(player)
    saveData(player)
end)

game:BindToClose(function()
    for i, player in pairs(game.Players:GetPlayers()) do
        saveData()
    end
end)

The following code is the part not working:

if success then

        print("Got past first line (success).")

        for i, v in pairs(data) do --This is the part not working.

            print("Got past second line (success).")

            local Tool = game.ReplicatedStorage.WeaponsFolder:FindFirstChild(v)
            local WeaponsFolder = player:FindFirstChild("WeaponsFolder")
            local ToolClone = Tool:Clone()
            ToolClone.Parent = WeaponsFolder

            print("Got past last line (success).")

    end
else
    print("Error: "..errorMessage)
end

It gives no error message when code has ran, but it only prints "Got past first line (success)."?

Any help will be highly appreciated :D

Answer this question