Problem with leaderstats script

Hello! I am trying to create a script that creates and saves a bunch of leaderstats. The creation part works. However, the saving part of said script isn’t working. I have been getting an error at line 44 saying “ServerScriptService.Leaderstats script:44: attempt to index number with ‘Points’” Scanning the script, I can’t seem to find any issues. Any help?

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

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

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Currency.Value = Data 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)
end)


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

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Points= Instance.new("IntValue", Leaderstats)
	Points.Name = "Points" 
	Points.Value = 0
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills" 
	Kills.Value = 0
	local Timealive= Instance.new("IntValue", Leaderstats)
	Timealive.Name = "Timealive" 
	Timealive.Value = 0
	local BestTime= Instance.new("IntValue", Leaderstats)
	BestTime.Name = "BestTime" 
	BestTime.Value = 0
	
	
	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Points.Value = Data.Points
		Kills.Value = Data.Kills
		Timealive.Value = Data.Timealive
		BestTime.Value = Data.BestTime
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Money"] = Player.leaderstats.Points.Value;
		["Kills"] = Player.leaderstats.Kills.Value; 
		["Timealive"] = Player.leaderstats.TimeAlive.Value; 
		["BestTime"] = Player.leaderstats.BestTime.Value; 
	})
end)
2 Likes

You are saving it has “Money”, but trying to load it as “Points”.
Change one of them to the other and it should work

I changed that, and it looks like it still has the same error. Just to confirm, but is it supposed to be a script in ServerScriptStorage? (I spent way too long one time trying to find an error for it to be in the wrong area)

Should be in serverscriptservice, yeah

Can you please send your updated code?
Also, you may need to join and rejoin a couple times for the new key to take place

Sure, it’s this:

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

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

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Currency.Value = Data 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)
end)


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

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Points= Instance.new("IntValue", Leaderstats)
	Points.Name = "Points" 
	Points.Value = 0
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills" 
	Kills.Value = 0
	local Timealive= Instance.new("IntValue", Leaderstats)
	Timealive.Name = "Timealive" 
	Timealive.Value = 0
	local BestTime= Instance.new("IntValue", Leaderstats)
	BestTime.Name = "BestTime" 
	BestTime.Value = 0


	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Points.Value = Data.Points
		Kills.Value = Data.Kills
		Timealive.Value = Data.Timealive
		BestTime.Value = Data.BestTime
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Points"] = Player.leaderstats.Points.Value;
		["Kills"] = Player.leaderstats.Kills.Value; 
		["Timealive"] = Player.leaderstats.TimeAlive.Value; 
		["BestTime"] = Player.leaderstats.BestTime.Value; 
	})
end)

You’re setting the datastore twice, with 2 differrent data points.
Try removing

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

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

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Currency.Value = Data 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Points.Value)
end)

from the script, and it may result in the desired outcome.

i.e.

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

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Points= Instance.new("IntValue", Leaderstats)
	Points.Name = "Points" 
	Points.Value = 0
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills" 
	Kills.Value = 0
	local Timealive= Instance.new("IntValue", Leaderstats)
	Timealive.Name = "Timealive" 
	Timealive.Value = 0
	local BestTime= Instance.new("IntValue", Leaderstats)
	BestTime.Name = "BestTime" 
	BestTime.Value = 0


	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Points.Value = Data.Points
		Kills.Value = Data.Kills
		Timealive.Value = Data.Timealive
		BestTime.Value = Data.BestTime
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Points"] = Player.leaderstats.Points.Value;
		["Kills"] = Player.leaderstats.Kills.Value; 
		["Timealive"] = Player.leaderstats.TimeAlive.Value; 
		["BestTime"] = Player.leaderstats.BestTime.Value; 
	})
end)

Looks like it’s still not working. I also just tried disabling all the other scripts incase something was interfering and it still won’t work

can you add print(Data) after local Data = Datastore:Get....

Alright, so tested it by going onto the game’s site and it does look like what I think you thought it was. Roblox Studio decided to not save the data but it seems to be working on the game itself.

You forgot to save on game restart which is why it doesn’t save in studio.

Code:

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

local function SaveData(player, data)
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId, data)
	end)
	
	if not success then
		warn(errorMessage)
	end
end

Players.PlayerAdded:Connect(function(Player)
	local Data = DataStore:GetAsync(Player.UserId) or {
		Points = 0,
		Kills = 0,
		Timealive = 0,
		BestTime = 0
	}
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points" 
	Points.Value = Data.Points
	Points.Parent = Leaderstats

	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills" 
	Kills.Value = Data.Kills
	Kills.Parent = Leaderstats

	local Timealive = Instance.new("IntValue")
	Timealive.Name = "Timealive" 
	Timealive.Value = Data.Timealive
	Timealive.Parent = Leaderstats

	local BestTime = Instance.new("IntValue")
	BestTime.Name = "BestTime" 
	BestTime.Value = Data.BestTime
	BestTime.Parent = Leaderstats
end)

Players.PlayerRemoving:Connect(function(Player)
	SaveData(Player, {
		Points = Player.leaderstats.Points.Value;
		Kills = Player.leaderstats.Kills.Value; 
		Timealive = Player.leaderstats.TimeAlive.Value; 
		BestTime = Player.leaderstats.BestTime.Value; 
	})
end)

game:BindToClose(function()
	for i, player in Players:GetPlayers() do
		SaveData(player, {
			Points = player.leaderstats.Points.Value;
			Kills = player.leaderstats.Kills.Value; 
			Timealive = player.leaderstats.TimeAlive.Value; 
			BestTime = player.leaderstats.BestTime.Value; 
		})
	end
end)