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

DataStore That Saves Hats Does Not Work?

Asked by 6 years ago

I have a folder located within the player where it is used to store hats the player gets. I made this script and put it into workspace (also tried ServerScriptStorage) both didn't work. The script is a regular script...

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HatSave")

game.Players.PlayerAdded:Connect(function(player)
       local hats = player:WaitForChild("Hats"):GetChildren()

        for _, child in pairs(hats) do
            child.Parent = ds:GetAsync(player.UserId)
        ds:SetAsync(player.UserId, child.Parent)
        end
end)


game.Players.PlayerRemoving:Connect(function(player)
        local hats = player.Hats:GetChildren()

        for _, child in pairs(hats) do
        ds:SetAsync(player.UserId, child.Parent)
        end
    end)

I don't know how efficient this method is, but feedback is always good

0
You can't save Instances, you can only save Strings, Numbers, and Booleans. RubenKan 3615 — 6y

1 answer

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

Your method is inefficient. What you could do is make an array, put all the hats in the array, then make the DataStoreService save the data.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("HatSave")

game.Players.PlayerAdded:Connect(function(player)
    --We'll just print the hats saved

    local data = dss:GetAsync(player.UserId) or {}

    for _,v in pairs(data) do
        print(v.Name)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local hatschildren = player:WaitForChild('Hats'):GetChildren()
    local hats = {}

    for _,v in pairs(hatschildren) do
        hats[#hats+1] = v.Name
    end

    -- Your error handler
    local success,message = pcall(function()
        ds:SetAsync(player.UserId, hats)
    end)
    if success then
        print("Successfully saved "..player.Name.."'s data")
    else
        warn("Failed to save "..player.Name.."'s data. :"..message)
    end
end)

If there's any errors, please mention

Ad

Answer this question