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?
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local function saveData(player)
local tableToSave = {
player.Character:WaitForChild("Values").Name.Value;
player.Character:WaitForChild("Values").Surname.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
local character = player.Character or player.CharacterAdded:Wait()
local Name = player.Character:WaitForChild("Values").Name.Value
local Surname = player.Character:WaitForChild("Values").Surname.Value
local Names = {"Guren", "Yuuichiro"}
local randomName = Names[math.random(1, #Names)]
local Surnames = {"Ichinose", "Hyakuya"}
local randomSurname = Surnames[math.random(1, #Surnames)]
if success and Name ~= nil then
Name = data[1]
Surname = data[2]
print(Name, Surname)
else
Surname = tostring(randomSurname)
Name = tostring(randomName)
print(Name, Surname)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
