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

My DataStore script will not save [?]

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TheData")
local warns = game.StarterGui.Warnings
local warnings = warns.Frame.WarnText



game.Players.PlayerAdded:Connect(function(player)
	
	
	local stats = Instance.new("Folder",player)
	stats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",stats)
	coins.Name = "Coins"
	coins.Value = 100

	local levels = Instance.new("IntValue", stats)
	levels.Name = "Levels"
	levels.Value = 1
	local exp = Instance.new("IntValue", stats)
	exp.Name = "Exp"
	exp.Value = 0
	local mycafeval = Instance.new("NumberValue", stats)
	mycafeval.Name = "OwnsCafe"
	mycafeval.Value = 0
	
	
	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
	
	if success then 
		coins.Value = data[1]
		levels.Value = data [2]
		exp.Value = data [3]
		
	else
		print("Player has NO data!")
		warnings.Text = "No data to be saved!"

	end

end)
local function saveData(player)
	local tableForSaving = {
		player.leaderstats.coins.Value,
		player.leaderstats.levels.Value,
		player.leaderstats.exp.Value
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableForSaving)

	end)

	if success then
		print("Data has been saved")
		if success then
			print("Data has been saved")
			warnings.Text = "Data has been saved!"

		else
			print("Failed To Save Data")
			warnings.Text = "Data has not been saved!"
			warn(err)
		end
	end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
		end)
		
		if success then
		print("Data has been saved")
		warnings.Text = "Data has been saved!"
		else
		print("Data has not been saved!")
		warnings.Text = "Data has not been saved!"

	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
			warnings.Text = "Data has been saved!"
		else
			print("Data has not been saved!")
			warnings.Text = "Data has not been saved!"
		end
	end
end)

This is my DataSave script but it will not save

2 comments

0

Please provide more context.

0

My script won't work and I don't know why what other context do you need

1 answer

Idk sure but you should try to separe the data save from leaderstats. It should work then.

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