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

What's wrong with this datastore code and how can I fix it?

Asked by 6 years ago

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:

01local DataStore = game:GetService("DataStoreService"):GetDataStore("CustomizationData")
02 
03game.Players.PlayerAdded:Connect(function(player)
04    wait(1)
05    --Creating the customization values for the player
06    local Folder = Instance.new("Folder", player)
07    Folder.Name = (player.Name.."Values")
08 
09    local SkinTone = Instance.new("IntValue", Folder)
10    SkinTone.Name = "SkinTone"
11 
12    local HairOne = Instance.new("IntValue", Folder)
13    HairOne.Name = "HairOne"
14 
15    local HairTwo = Instance.new("IntValue", Folder)
View all 68 lines...
0
Use BindToClose greatneil80 2647 — 6y

1 answer

Log in to vote
0
Answered by
rower86 35
6 years ago

I suggest making the function that begins at line 55 a variable for this, your problem is that the saving code doesn't run before the game closes when the last player of a server leaves. To fix this use the built in function BindToClose. BindToClose fully runs a code right before the server closes.

01local function SavePlayerStats(player)
02        local Key = (player.Name.."Data")
03        local Folder = player:FindFirstChild(player.Name.."Values")
04        local SkinTone = Folder.SkinTone
05        print (SkinTone.Value)
06        local HairOne = Folder.HairOne
07        local HairTwo = Folder.HairTwo
08        local HairColour = Folder.HairColour
09        local Shirt = Folder.Shirt
10        local Pants = Folder.Pants
11        print(SkinTone.Value)
12        print("Saved")
13        DataStore:SetAsync(Key, {SkinTone.Value, HairOne.Value, HairTwo.Value, HairColour.Value, Shirt.Value, Pants.Value})
14end
15 
View all 24 lines...
0
`SetAsync` yields, so if it takes too long and there's a few players, some might not get their data saved; the solution's to use a coroutine. TheeDeathCaster 2368 — 6y
0
Even after implementing this into it, it's still failing to save the data. Any other solutions? Archimeus 0 — 6y
0
Try using this method - https://www.youtube.com/watch?v=PhtniVQbwlk rower86 35 — 6y
Ad

Answer this question