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

How do you saves clothes in datastore?

Asked by
Zoltra 4
4 years ago

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)
0
Also I get the error: ServerScriptService.clothesData:7: attempt to index nil with 'WaitForChild' Zoltra 4 — 4y
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. ede2355 22 — 4y
0
I'm getting attempt to index nil with number. At line 14 Zoltra 4 — 4y
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. Zoltra 4 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

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. SteamG00B 1633 — 4y
1
To wait for the character's children to load, you need to do repeat wait() until player:HasAppearanceLoaded() SteamG00B 1633 — 4y
Ad

Answer this question