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

My DataStore's Will Not Save Across Places [Why??]

So I'm currently making an RPG game. In the game you have stats such as your level, experience, and stuff like that. You also have the ability to change your player's look. Therefore, I need to save their looks and stats.

I have 2 places, both in the same game. They both use the exact same script and with the EXACT same DataStore. However, when I am teleported from one place to the next after creating my character in the first, my DataStore is not loaded whatsoever. No errors, nothing.

I'll show the script that is in both places, parented to ServerScriptService. I need to why the data is not loaded on the second place. This is crucial to my game's success. Any help would be very appreciated.

ServerData (script):

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore = DataStoreService:GetDataStore('PlayerStats_A')





--//Settings\\--
local StarterData = {	
	Race = "Human";
	Exp = 0;
	Level = 1;
	Magic = false;
	Melee = true;
	Sprint = 23;
	Walk = 16;
	BaseDamage = 0.45;
	Coins = 0;
	SkillPoints = 0;
	MaxHealth = 25;
	HealthSkills = 0;
	DamageSkills = 0;
	SpeedSkills = 0;
	Playing = false;
	Customizing = false;
	HairColor = "Black";
	TempPrimaryColor = "Pastel violet";
	TempSecondaryColor = "Medium stone grey";
	TempPantsColor = "Flint";
	TempHairName = "Bald";
	HairName = "Bald";
	EyesName = "Eyes2";
	MouthName = "Mouth7";
	HasPlayed = false;
};

local playersavetable = {};
local AUTO_SAVE_INTERVAL = 180;

--//Functions\\--
local function loadStaterData(Player)
	local Stats = Instance.new("Folder")
	Stats.Name = "Data"
	Stats.Parent = Player
	for statname, statvalue in pairs(StarterData) do
		if type(statvalue) == 'number' then
			local intvalue = Instance.new("IntValue")
			intvalue.Name = statname
			intvalue.Value = statvalue
			intvalue.Parent = Stats
		elseif type(statvalue) == 'boolean' then
			local boolvalue = Instance.new("BoolValue")
			boolvalue.Name = statname
			boolvalue.Value = statvalue
			boolvalue.Parent = Stats
		elseif type(statvalue) == 'string' then
			local stringvalue = Instance.new("StringValue")
			stringvalue.Name = statname
			stringvalue.Value = statvalue
			stringvalue.Parent = Stats
		end
	end
end

local function loadData(Player)
	local Data
	local s, e = pcall(function()
		Data = DataStore:GetAsync('UserId:'..Player.UserId)
	end)

	if s then
		print('Getting '..Player.Name.."'s data was successful")
	else
		warn('Something weng wrong when loading'..Player.Name.."'s data")
	end

	if Data then
		for statname, statvalue in pairs(Data) do
			Player.CharacterData[statname].Value = statvalue
		end
		print(Player.Name.."'s data has been loaded!")
	else
		print(Player.Name..'has no data! Generating new data.')
		game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("GenerateData"):FireClient(Player)
	end
end

local function saveData(Player)
	if RunService:IsStudio()then return end
	local Data = {}
	for _, stat in pairs(Player.CharacterData:GetChildren()) do
		Data[stat.Name] = stat.Value
	end
	local s, e = pcall(function()
		DataStore:SetAsync('UserId:'..Player.UserId, Data)
	end)
	if s then
		print(Player.Name.."'s data has been successfully saved")
	else
		print(e)
		warn('Something went wrong while saving '..Player.Name.."'s DATA")
	end
end


Players.PlayerAdded:Connect(function(Player)
	playersavetable[Player] = tick()
	loadStaterData(Player)
	loadData(Player)
end)

Players.PlayerRemoving:Connect(function(Player)
	saveData(Player)
end)

game:BindToClose(function()
	wait(5)
end)
while true do
	wait(1)
	for _, Player in pairs (Players:GetPlayers()) do
		if tick() - playersavetable[Player] >= AUTO_SAVE_INTERVAL then
			saveData(Player)
			playersavetable[Player] = tick()
		end
	end	
end

1 comment

0

Your datastore will not save across places because your datastore is located in one game. It can't be accessed by other games.

1 answer

Try This

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Stage
	local save2 = plr.leaderstats.Coins

	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Stage.Value, plr.leaderstats.Coins.Value})
end)

2 comments

0

That's pretty much exactly what I have already written except without the automated features :|

0

Did you turn on API Service

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