DataStore not saving data correctly?
I've been trying to create a random name generator for all players whenever they firstly join the game, then save it through DataStore. However, each times I play the game I will just get a new name EVEN if the output says (4 times lol) the data has been saved. Help?
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local dataStore = DataStoreService:GetDataStore( "MyDataStore" ) |
04 | local function saveData(player) |
07 | player.Character:WaitForChild( "Values" ).Name.Value; |
08 | player.Character:WaitForChild( "Values" ).Surname.Value |
11 | local success, err = pcall ( function () |
12 | dataStore:SetAsync(player.UserId, tableToSave) |
16 | print ( "Data has been saved!" ) |
18 | print ( "Data hasn't been saved!" ) |
23 | game.Players.PlayerAdded:Connect( function (player) |
25 | local success, err = pcall ( function () |
27 | data = dataStore:GetAsync(player.UserId) |
31 | local character = player.Character or player.CharacterAdded:Wait() |
32 | local Name = player.Character:WaitForChild( "Values" ).Name.Value |
33 | local Surname = player.Character:WaitForChild( "Values" ).Surname.Value |
34 | local Names = { "Guren" , "Yuuichiro" } |
35 | local randomName = Names [ math.random( 1 , #Names) ] |
36 | local Surnames = { "Ichinose" , "Hyakuya" } |
37 | local randomSurname = Surnames [ math.random( 1 , #Surnames) ] |
39 | if success and Name ~ = nil then |
45 | Surname = tostring (randomSurname) |
46 | Name = tostring (randomName) |
52 | game.Players.PlayerRemoving:Connect( function (player) |
53 | local success, err = pcall ( function () |
58 | print ( "Data has been saved" ) |
60 | print ( "Data has not been saved!" ) |
64 | game:BindToClose( function () |
65 | for _, player in pairs (game.Players:GetPlayers()) do |
66 | local success, err = pcall ( function () |
71 | print ( "Data has been saved" ) |
73 | print ( "Data has not been saved!" ) |