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

How could you save and load players clothing to a data store?

Asked by 3 years ago

The following code is what I currently have in a server script within ServerScriptService. I have tried to finesse this code up and down, and cannot find a solution to save and load player's clothing when they leave and rejoin the game. Another thing I have noticed with this code, is that the game doesn't seem to read SetAsync or GetAsync, since no pcall function results are showing up in the output console. Please help, thank you very much!

local OutfitDataStore = game:GetService("DataStoreService"):GetDataStore("OutfitDataStore") local ServerStorage = game:GetService("ServerStorage") local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player) local character = player.Character or player.CharacterAdded:Wait()

    local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
    local pantsId = player.Character:WaitForChild("Pants").PantsTemplate

    local data
    local success, errormessage = pcall(function()
        data = OutfitDataStore:GetAsync(player.userId)
    end)
    if success then
        shirtId = data[1]
        pantsId = data[2]
    else
        print("No outfit to load from data store")
        warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(player) local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate local pantsId = player.Character:WaitForChild("Pants").PantsTemplate

        local success, errormessage = pcall(function()
                OutfitDataStore:SetAsync(player.userId,{shirtId, pantsId})
        end)
        if success then
            print ("Player Data successfully saved")
        else
            print ("Error saving data")
            warn(errormessage)
    end

end)

1 answer

Log in to vote
0
Answered by 3 years ago

You can't save objects to datastores, but you can do this. If you are saving a part, you can save if its anchored value or its color. Hope this helps!

Ad

Answer this question