Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you saves clothes in datastore?

Hey I'm a new scripter and I'd appreciate any help. Giving some background, the player's clothes change in server. I'm trying to make a script that when the player leaves, it saves those clothes and when they return they have those same clothes. Trying to figure out datastore, but struggling quite a bit.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
    local shirtId = player.Character:WaitForChild("Shirt").ShirtTemplate
    local pantsId = player.Character:WaitForChild("Pants").PantsTemplate
    
    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId, {shirtId, pantsId})
    end)
    if success then
        shirtId = data[1]
        pantsId = data[2]
    else
        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()
        myDataStore:SetAsync(player.UserId,{shirtId, pantsId})
    end)
    if success then
        print ("Player Data successfully saved")
    else
        print ("Error saving data")
        warn(errormessage)
    end
end)

4 comments

0

Also I get the error: ServerScriptService.clothesData:7: attempt to index nil with 'WaitForChild'

0

I believe that WaitForChild error is due to the character not fully loading in before the script does, that's just a guess. But also I think it does save the clothes if you don't get that one error, all you have to do is apply the id to either a pants object or a shirt object.

0

I'm getting attempt to index nil with number. At line 14

0

Just in case I didn't explain what I'm trying to do properly. Players clothes change in-game. I want to save it and have it load next time they join the game.

1 answer

You made a few mistakes with the datastore: first of all, getAsync has only one paramter and thats the key, secondly you need a default value incase you haven't stored a value yet for this person in that case it will return a nil so when u get a nil u give the default value

 local data
local success, errormessage = pcall(function()
  data = myDataStore:GetAsync(player.UserId )
end)

about getAsync

Next like the person said in the comments you need to wait for the character to exist first before you ask its children.

player.CharacterAdded:wait()

see this article about datastores

2 comments

0

That is not the correct way to wait for a character to load. That event fires when the character model is created, not when the character fully loads.

1

To wait for the character's children to load, you need to do repeat wait() until player:HasAppearanceLoaded()

This discussion is preserved as it was. New answers, comments, and votes are closed.