Leaderstats issue - attempt to index numbers with 'Wins'

Hey so I have a problem in my leaderstats script. I want two values ; Wins and Money

l

ocal 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 Currency = Instance.new("IntValue", Leaderstats)
	Currency.Name = "Money" -- Change "Money" with your currency.
	Currency.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Currency.Value = Data -- Change this if you have added more currencies.
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Money.Value) -- Change "Money" with your currency.
end)

-- 2 OR MORE CURRENCIES (DON'T COPY THIS DOWN HERE IF YOU ARE GOING TO USE ONLY ONE CURRENCY) --
-- IN THIS EXAMPLE I USED 2 CURRENCIES, BUT YOU CAN ADD MORE.

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 Money= Instance.new("IntValue", Leaderstats)
	Money.Name = "Money" -- Change "Money" with your currency.
	Money.Value = 0
	local Wins= Instance.new("IntValue", Leaderstats)
	Wins.Name = "Wins" -- Change "Wins" with your currency.
	Wins.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Wins.Value = Data.Wins -- Change "Wins" with your currency.
		Money.Value = Data.Money -- Change "Money" with your currency.
		
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Money"] = Player.leaderstats.Money.Value; -- Change "Money" with your currency.
		["Wins"] = Player.leaderstats.Wins.Value; -- Change "Kills" with your currency.
	})
end)
1 Like

Can you tell me more about your problem? What line does the error happen on etc.

Why are you connecting 2 PlayerAdded and 2 PlayerRemoving events?

The error is caused by this line, because you’re first saving “Money” value and not the table. Just use an array and 1 event of each.

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

local function SaveData(Player)
	local Data = {
		Player.leaderstats.Money.Value;
		Player.leaderstats.Wins.Value
	}

	DataStore:SetAsync(Player.UserId, Data)
end

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

	local Data
	
	local Success, Error = pcall(function()
		Data = DataStore:GetAsync(Player.UserId) or {}
	end)
	
	if Success then
		Wins.Value = Data and Data[1] or 0
		Money.Value = Data and Data[2] or 0
	end
end)

Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, Player in ipairs(Players:GetPlayers()) do
		SaveData(Player)
	end
end)

This line doesn’t seem to be functioning

Hey getting attempt to index number with number at line 37

if Success then
		Wins.Value = Data and Data[1] or 0
		Money.Value = Data and Data[2] or 0

It works fine for me, it’s probably because of the last save, try initializing the server again.
image

Still doesn’t work. Here is the script ;

still getting attempt to index number with number


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

local function SaveData(Player)
	local Data = {
		Player.leaderstats.Money.Value;
		Player.leaderstats.Wins.Value
	}

	DataStore:SetAsync(Player.UserId, Data)
end

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

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

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

	local Data

	local Success, Error = pcall(function()
		Data = DataStore:GetAsync(Player.UserId) or {}
	end)

	if Success then
		Wins.Value = Data and Data[1] or 0
		Money.Value = Data and Data[2] or 0
	end
end)

Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, Player in ipairs(Players:GetPlayers()) do
		SaveData(Player)
	end
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

local function SaveData(Player)
	local Money = Player.leaderstats.Money.Value
	local Wins = Player.leaderstats.Wins.Value
	local Data = {Money, Wins}
	
	local Success, Error = pcall(function()
		DataStore:SetAsync(Player.UserId, Data)
	end)
end

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

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

	local Data

	local Success, Error = pcall(function()
		Data = DataStore:GetAsync(Player.UserId)
	end)
	
	if type(Data) == table then
		Money.Value = Data[1]
		Wins.Value = Data[2]
	end
end)

Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, Player in ipairs(Players:GetPlayers()) do
		SaveData(Player)
	end
end)