What's wrong with this datastore code and how can I fix it?
I've been trying to make a character customization GUI in which all of the character values save.
However, everything works fine until when the player leaves. When they leave, the values don't update, they stay the same as they were previously when I joined the game.
When I use a datastore editor plugin, and change them, they load as the values I changed them to, but when I change them in studio or in the game, the values appear to change but they still don't save when I leave.
Here's my datastore code:
01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( "CustomizationData" ) |
03 | game.Players.PlayerAdded:Connect( function (player) |
06 | local Folder = Instance.new( "Folder" , player) |
07 | Folder.Name = (player.Name.. "Values" ) |
09 | local SkinTone = Instance.new( "IntValue" , Folder) |
10 | SkinTone.Name = "SkinTone" |
12 | local HairOne = Instance.new( "IntValue" , Folder) |
13 | HairOne.Name = "HairOne" |
15 | local HairTwo = Instance.new( "IntValue" , Folder) |
16 | HairTwo.Name = "HairTwo" |
18 | local HairColour = Instance.new( "IntValue" , Folder) |
19 | HairColour.Name = "HairColour" |
21 | local Shirt = Instance.new( "IntValue" , Folder) |
24 | local Pants = Instance.new( "IntValue" , Folder) |
27 | local Key = (player.Name.. "Data" ) |
30 | local Get_Data = DataStore:GetAsync(Key) |
32 | SkinTone.Value = Get_Data [ 1 ] |
33 | HairOne.Value = Get_Data [ 2 ] |
34 | HairTwo.Value = Get_Data [ 3 ] |
35 | HairColour.Value = Get_Data [ 4 ] |
36 | Shirt.Value = Get_Data [ 5 ] |
37 | Pants.Value = Get_Data [ 6 ] |
46 | for i, v in pairs (Folder:GetChildren()) do |
47 | v.Changed:Connect( function () |
48 | DataStore:SetAsync(Key, { SkinTone.Value, HairOne.Value, HairTwo.Value, HairColour.Value, Shirt.Value, Pants.Value } ) |
49 | print ( "Data changed, set" ) |
55 | game.Players.PlayerRemoving:Connect( function (player) |
56 | local Key = (player.Name.. "Data" ) |
57 | local Folder = player:FindFirstChild(player.Name.. "Values" ) |
58 | local SkinTone = Folder.SkinTone |
59 | print (SkinTone.Value) |
60 | local HairOne = Folder.HairOne |
61 | local HairTwo = Folder.HairTwo |
62 | local HairColour = Folder.HairColour |
63 | local Shirt = Folder.Shirt |
64 | local Pants = Folder.Pants |
67 | DataStore:SetAsync(Key, { SkinTone.Value, HairOne.Value, HairTwo.Value, HairColour.Value, Shirt.Value, Pants.Value } ) |