Problem with datastore

That and http requests are needed, and yes I have them both enabled.

Have you made this yourself? if so I could help! if not then I still can!

You should use :FindFirstChild() to do this.

Yes (the game with a team the scripts and guis myself). How would you help, me sending you the script, or letting you into a team create on our game.

Yep I would do that or I could simply just connect a db to save the leaderboard manually cause I’m into API instead and that :smiley:

You shouldn’t really trust people easily so you only wanted to save the leaderboard right and have them on?

Try this!

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

game.Players.PlayerAdded:Connect(function(plr)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local EP = Instance.new("IntValue")
	EP.Name = "Evolution Points"
	EP.Parent = leaderstats

	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = leaderstats

	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats

	local key = plr.UserId

	local data
	local success,msg = pcall(function()
		data = DataStore:GetAsync(key)
	end)

	if data then
		print("Data Loaded")
		EP.Value = data.E
		XP.Value = data.X
		Level.Value = data.L
	else
		print("Error")
		warn(msg)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId

	local data = {
		E = player.leaderstats["Evolution Points"].Value,
		X = player.leaderstats.XP.Value,
		L = player.leaderstats.Level.Value,
	}

	local success,msg = pcall(function()
		DataStore:SetAsync(key,data)
	end)

	if success then
		print("Data Saved!")
	end
end)
1 Like

I understand that you’ve used alvinblox or someones tutorial so I will be posting a fix as fast as I can.

1 Like

It gets rid of the error and says that it has saved, but… It doesn’t save.

What script is like changing the value?

There is no other script changing the values. For this test I have everything else that changes the values set to disabled.

Have you thought about saving it to their userid manually instead?

no, how would I do that? via auto save? If so then how do you change the data manually?

Enable to scripts and test it to see if it works!

It still doesn’t work… It looks like it is getting close though.

Try testing it in like the actual game instead of studio!

1 Like

I’ve tried this and tested it out but it’s straightforward.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") -- Change this with a different name.

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local XP = Instance.new("IntValue", Leaderstats)
	XP.Name = "XP" 
	XP.Value = 0

	local EvolutionPoints = Instance.new("IntValue", Leaderstats)
	EvolutionPoints.Name = "Evolution Points"
	EvolutionPoints.Value = 0
	
	local Level = Instance.new("IntValue", Leaderstats)
	Level.Name = "Level" 
	Level.Value = 0
	

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		XP.Value = Data.XP -- Change to "XP" to ur currency
		EvolutionPoints.Value = Data.EvolutionPoints -- Change "EvolutionPoints" with your currency.
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["XP"] = Player.leaderstats.XP.Value;
		["Level"] = Player.leaderstats.XP.Value;-- Change "Level" with your currency.
		["Evolution Points"] = Player.leaderstats.EvolutionPoints.Value; -- Change "EvolutionPoints" with your currency.
	})
end)
1 Like

Seems to me your not using “BindToClose”, whenever I use datastores. I always put the bindtoclose function right after the game.Players.PlayerRemoving function.

Try this:

local DSS = game:GetService("DataStoreService")
local MDS = DSS:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local EP = Instance.new("IntValue")
	EP.Name = "Evolution Points"
	EP.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = leaderstats
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats
	
	local data
	local data2
	local data3
	local success, errormessage = pcall(function()
		data = MDS:GetAsync(plr.UserId.."-Evolution Points")
		data2 = MDS:GetAsync(plr.UserId.."-XP")
		data3 = MDS:GetAsync(plr.UserId.."-Level")
	end)
	
	if success then
		EP.Value = data
		XP.Value = data2
		Level.Value = data3
	else
		print("Failed!")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		MDS:SetAsync("id_"..plr.UserId, plr.leaderstats["Evolution Points"].Value, plr.leaderstats.XP.Value, plr.leaderstats.Level.Value)
	end)
	
	if success then
		print("Success!")
	else
		print("Failed!")
		warn(errormessage)
	end
end)

game:BindToClose(function()
    wait(1)
end)

Read this, if you want to. It helped me realize the same thing: DataStores - Beginners to Advanced

1 Like

The reason your script wasn’t working was because your “SetAsync()” call was made with 4 values passed as arguments to it when it only expects 2 arguments, because of this the additional 2 values (which were 2 of the leaderstat’s stats) were discarded and not saved. Instead of attempting to save/load each stat’s value individually you can instead collect all of the values into a single table value (array) and save/load that to/from the DataStore.

local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("DataStore")

local ProtectedCall = pcall

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player

	local EP = Instance.new("IntValue")
	EP.Name = "Evolution Points"
	EP.Parent = Leaderstats

	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = Leaderstats

	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = Leaderstats
	
	local Success, Result = ProtectedCall(function()
		return DataStore:GetAsync(Player.UserId.."_Stats")
	end)
	
	if Success then
		if Result then
			if type(Result) == "table" then
				EP.Value = Result[1]
				XP.Value = Result[2]
				Level.Value = Result[3]
			end
		end
	else
		warn(Result)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local Success, Result = ProtectedCall(function()
		return DataStore:SetAsync(Player.UserId.."_Stats", {Player.leaderstats["Evolution Points"].Value, Player.leaderstats.XP.Value, Player.leaderstats.Level.Value})
	end)
	
	if Success then
		print(Result)
	else
		warn(Result)
	end
end)

game:BindToClose(function()
	for _, Player in ipairs(Players:GetPlayers()) do
		local Success, Result = ProtectedCall(function()
			return DataStore:SetAsync(Player.UserId.."_Stats", {Player.leaderstats["Evolution Points"].Value, Player.leaderstats.XP.Value, Player.leaderstats.Level.Value})
		end)

		if Success then
			print(Result)
		else
			warn(Result)
		end
	end
end)
1 Like

I did not know that (always learning new things). Thanks! I’ll try it.

1 Like