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

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?

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)

2 answers

✓ Accepted answer

This should fix the error you're having, let me know if there are still any errors.

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore3")
local players = 0

game.Players.PlayerAdded:Connect(function(player)
	
	players = players + 1
	local key = "Player_"..player.UserId
	
	local data 
	local success, err = pcall(function()

		data = dataStore:GetAsync(key)

	end)

	local character = player.Character or player.CharacterAdded:Wait()
	local Name = player.Character:WaitForChild("Values"):FindFirstChild("Name")
	local Surname = player.Character:WaitForChild("Values"):FindFirstChild("Surname")
	local Names = {"Guren", "Yuuichiro"}
	local randomName = Names[math.random(1, #Names)]
	local Surnames = {"Ichinose", "Hyakuya"}
	local randomSurname = Surnames[math.random(1, #Surnames)]

	if data then
		Name.Value = data["Name"]
		Surname.Value = data["Surname"]
		print("loaded previous data "..data["Name"].." "..data["Surname"])
	else        
		Surname.Value = randomSurname
		Name.Value = randomName
		print("loads new data "..Name.Value.." "..Surname.Value)
	end

end)

local function create_table(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local player_stats = {}
	for _, stat in pairs(character:FindFirstChild("Values"):GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player) 
	local player_stats = create_table(player)
	local key = "Player_"..player.UserId -- this is the key we use to save the data
	
	local success, err  = pcall(function()
		dataStore:SetAsync(key, player_stats)
		players = players - 1
		bindableEvent:Fire()
	end)

	if success then
		print("Data has been saved")
	else
		warn(err)
	end
end)

game:BindToClose(function()
	while players > 0 do
		bindableEvent.Event:Wait()
	end
end)

3 comments

0

It does! Thank you a lot. <3 (no homo)

0

If I want to change my name, do I just use the server output to modify the name value in my character?

0

Yes, you could do it via remote-events, and if you want to reset stats just do this. dataStore:RemoveAsync(key)

There is nothing wrong with this script. It is the script of how you set those values

2 comments

0

There is no script that sets those values, as I aforementioned the values are all set in the script and require no other script.

0

There is no script that sets those values, as I aforementioned the values are all set in the script and require no other script.

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