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

DataStore fails to save values?

I've been working on finding out how to DataStore something not on the leaderstats, I came up with this but it does not save?

local DataStoreService = game:GetService("DataStoreService")
local save = DataStoreService:GetDataStore("save")




game.Players.PlayerAdded:Connect(function(player)
	local DataValues = Instance.new("Folder")
	DataValues.Name = "DataValues"
	DataValues.Parent = player
	
	local KoalaCounter = Instance.new("IntValue")
	KoalaCounter.Name = "KoalaCounter"
	KoalaCounter.Parent = DataValues

	local Data2
	
	local success, err = pcall(function()
		
		Data2 = save:GetAsync(player.UserId.."-KoalaPoints", KoalaCounter.Value)
		
	end)
	if success then
		
		KoalaCounter.Value = Data2
		
		print("Success")
		
	else
			print("Error getting data")
			warn(err)
	end
	
	end)



game.Players.PlayerRemoving:Connect(function(player)
	
	
	local success, err = pcall(function()
		
		save:SetAsync(player.UserId.."-KoalaPoints", player.DataValues.KoalaCounter.Value)
		
	end)
	
	if success then
		print ("data saved")
	else
			print("There was an error")
			warn(err)
	end
	
end)

1 answer

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("DataStore") 

game.Players.PlayerAdded:Connect(function(player)
	
	local datastuff = Instance.new("Folder")
	datastuff.Name = "DataStuff"
	datastuff.Parent = player
	
	local KoalaCounter = Instance.new("IntValue")
	KoalaCounter .Name = "KoalaCounter "
	KoalaCounter .Parent = datastuff
	
	local playerUserId = "Player_"..player.UserId
	
	--below this loads data
	--pcall wrap allows the data to not get corrupt during roblox mess ups
	
	local data --globalised data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
		
	if success then
		KoalaCounter .Value = data.KoalaCounter 
	end		
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {
		KoalaCounter = player.DataStuff.KoalaCounter .Value
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
		
	if success then
		print("Data Successfully saved!")
	else
		print("There was an error whilst saving data!")
		warn(errormessage)	
	end
		
end)

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