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

My DataStore will not save! Please help?

My points or values will not save what did I do wrong?

Datastore Script(in ServerScriptService)

local DS = game:GetService("DataStoreService"):GetDataStore("Levelz")
local StatFile = game:GetService("ServerStorage"):WaitForChild("StatFile")

game.Players.PlayerAdded:connect(function(player)
    local Key = "Player-"..player.userId
    
    local playerfolder = Instance.new("Folder", StatFile)
    playerfolder.Name = player.Name
    
    local strength = Instance.new("IntValue", playerfolder)
    strength.Name = "Strength"
    
    local defense = Instance.new("IntValue",playerfolder)
    defense.Name = "Defense"
    
    local sword = Instance.new("IntValue", playerfolder)
    sword.Name = "Sword"
    
    local slevel = Instance.new("IntValue", playerfolder)
    slevel.Name = "SLevel"
    
    local health = Instance.new("IntValue", playerfolder)
    health.Name = "Health"
    
    
    
    if DS:GetAsync(Key) then
        defense.Value = DS:GetAsync(Key)
        strength.Value = DS:GetAsync(Key)
        sword.Value = DS:GetAsync(Key)
        health.Value = DS:GetAsync(Key)
        slevel.Value = DS:GetAsync(Key)
    end

end)

game.Players.PlayerRemoving:connect(function(player)
    local Key = "Player-"..player.userId
    local AllStats = game.ServerStorage.StatFile[player.Name]:GetChildren()
    for i = 1, #AllStats do
        DS:SetAsync(Key,AllStats[i].Value, AllStats[i].Name)
    end
    
end)

1 answer

✓ Accepted answer

First of all, you don't need to check if the actual data exists, you're pretty much saving the statistics, so you don't have to check for DS:GetASync(Key) if it exists, straight up just save the data, because that if statement never runs. You're checking if the statistics of that player exist so if the player hasn't played before and it's saving for the first time that if statement won't run ever. So you'd straight directly save the data without checking anything.


 if DS:GetAsync(Key) then
        defense.Value = DS:GetAsync(Key)
        strength.Value = DS:GetAsync(Key)
        sword.Value = DS:GetAsync(Key)
        health.Value = DS:GetAsync(Key)
        slevel.Value = DS:GetAsync(Key)
    end

-- // Straight up save them.

defense.Value = DS:GetAsync(Key)
        strength.Value = DS:GetAsync(Key)
        sword.Value = DS:GetAsync(Key)
        health.Value = DS:GetAsync(Key)
        slevel.Value = DS:GetAsync(Key)

Also, instead of using actual values, which is a pretty unneficient way to store information you could use tables, which is a pretty organized way to actually store information for each player as an example.

-- // Definitions

local PLAYERS = game:GetService('Players');
local DATASTORE = game:GetService("DataStoreService"):GetDataStore("Levelz")


local data = {} -- Empty Table.

game.Players.PlayerAdded:Connect(functoin(player)
	local key = 'user_' ..player.userId
	
	DATASTORE:GetAsync(key);
	data[player] = {
		Statistics = {
			Strength = 0;
			Defense = 0;
			Sword = 0;
			SLevel = 0;
		};
		CharacterStatistics = {
			Health = 100;	
		};
	};

	
end);


local function remove(player)
	local key = 'user_' ..player.userId
	
	DATASTORE:SetAsync(key, data[player]); -- Save the information.
	
	data[player] = nil -- Remove the player when he leaves the game because it's unnecessary to keep his information when he has already left the game.
end;

PLAYERS.PlayerRemoving:Connect(remove);

game:BindToClose(function() -- Very useful event when the server entirely closes it basically unloads all the information stored in the data table.
	for player in next, data do -- for each player in `data`
		remove(player); -- remove the player from the table
	end;
end);


Also, GetAsync and SetAsync do not work as you might think they do. Check them out,

The key is the unique identification that a person has. As an example think the key as the ID CARD that every human has. And the SetAsync the pen which you use to write the information into the ID CARD.

GetAsync(key) : This basically gets the information that's stored in the UNIQUE ID CARD. SetAsync(key, Value) : This is used to write the information into the UNIQUE ID CARD, think it of as a pen that you use to write a specific "Value" inside the "Key" which is the UNIQUE CARD ID.

I recommend you to check the DataStore page and check the GetAsync and SetAsync Methods more closely.

If you don't know a few stuff that's used in this script you can find them here:

http://wiki.roblox.com/index.php?title=Data_store

http://wiki.roblox.com/index.php?title=API:Class/DataModel/BindToClose

http://wiki.roblox.com/index.php?title=API:Class/Players/PlayerRemoving

http://wiki.roblox.com/index.php?title=API:Class/Players/PlayerAdded

http://wiki.roblox.com/index.php?title=Function

http://wiki.roblox.com/?title=Table

Pretty much that was it, hope it was useful!

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