Problem with datastore

  1. What do you want to achieve? Keep it simple and clear!
    I want to save the players money, xp, and level.

  2. What is the issue? Include screenshots / videos if possible!
    It won’t save. It always comes up with this error;
    13:10:21.510 Unable to cast value to Object - Server - leaderstats:49

My code;

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)

I tried to make a different script to fix this problem, here is it’s code;

local DataStoreService = game:GetService("DataStoreService")
local StoreData = DataStoreService:GetDataStore("StoreleaderstatsData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.UserId
	local Save1 = plr.leaderstats:FindFirstChild("Evolution Points")
	local Save2 = plr.leaderstats:FindFirstChild("XP")
	local Save3 = plr.leaderstats:FindFirstChild("Level")
	
	local GetSaved = StoreData:GetAsync(plrkey)
	if GetSaved then
		Save1 = GetSaved[1]
		Save2 = GetSaved[2]
		Save3 = GetSaved[3]
	else
		local NumberForSaving = {Save1.Value, Save2.Value, Save3.Value}
		StoreData:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	StoreData:SetAsync("id_"..plr.UserId, {plr.leaderstats["Evolution Points"].Value, plr.leaderstats.XP.Value, plr.leaderstats.Level.Value})
end)

They are both serverscripts inside of serverscriptservice.

Is “EvolutionPoints” a string, and integer, or something else? And why are you referencing it with brackets instead of referencing it the same way you did with XP and Level?

Hello! You have to enable Access to API Services.You can find that in file, Game Settings, Security

1error I see in my game I can’t know if this works but hey here’s what I can see.

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)

Because it has a space in the name. You can’t call anything with a number or space without using brackets.

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.