Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStore seems not to work? Using it for random names generators.

I've been trying to create a random name generator for all players whenever thei firstly join the game, then save it through DataStore. However, each times I play the game it won't print the results and when leaving the game will freeze stating that "Script that implemented this callback has been destroyed". Help?

P.S. I put the script in StarterCharacterScripts.

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")

local function saveData(player)

	local tableToSave = {
		player:WaitForChild("Values").Name; 
		player:WaitForChild("Values").Surname 
	}

	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 Name = player.Values.Name
	local Surname = player.Values.Surname
	local Names = {"Guren", "Yuu", "Mikaela", "Yuuichiro"}
	local randomName = Names[math.random(1, #Names)]
	local Surnames = {"Ichinose", "Hyakuya", "Hiragi"}
	local randomSurname = Surnames[math.random(1, #Surnames)]
	
	if success then
		
		Name.Value = data[1]
		Surname.Value = 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)

1 answer

✓ Accepted answer

youre trying to save an object instead of a value, update that

This discussion is preserved as it was. New answers, comments, and votes are closed.