I need help with this datastore

i am using a datastore script. on the bottom the coins does not save but the wins do? i read line by line to make sure things were set. i did multiple tests as well. here is the script

local DSS = game:GetService('DataStoreService')
local myDataStore = DSS:GetDataStore('myDatastore')

game.Players.PlayerAdded:Connect(function(plr)
	--gets the int value which is wins 
	local wins = plr:WaitForChild('leaderstats').Wins
	
	local coins = plr:WaitForChild('leaderstats').Coins
	--create an empty variable to get the value of wins from the player
	local data
	local data2 
	
	local success, error = pcall(function()
		data = myDataStore:GetAsync(plr.UserId.."_wins", plr.leaderstats.Wins.Value)
		data2 = myDataStore:GetAsync(plr.UserId.."_coins", plr.leaderstats.Coins.Value)
	end)
	
	if success then
		wins.Value = data 
		print("Data has been reached!")
		coins.Value = data2
		print('Coins had been reached!')
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = plr.leaderstats.Wins.Value
	local data2 = plr.leaderstats.Coins.Value
	
	local success, error = pcall(function()
		myDataStore:SetAsync(plr.UserId.."_wins", plr.leaderstats.Wins.Value)
		myDataStore:SetAsync(plr.UserId.."_coins", plr.leaderstats.Coins.Value)
	end)
	
	if success then
		print("Saved data!")
	else
		print('Error!')
		warn(error)
	end
end)

at line 32 is where the coins are at and the coins dont actually print saved data and no errors are returned

It’s probably because you’re calling too many Datastore requests. Just save it as a table:

local ToSave = {
  plr.leaderstats.Wins.Value,
  plr.leaderstats.Coins.Value;
}
1 Like

how would i place that into my script. would i just replace the data variables from the playerremoving function?

Just save ToSave instead of saving two things.

Then, to retrieve:

local data = SomeStore:GetAsync(Key)
plr.leaderstats.Wins.Value = data[1] or 0
plr.leaderstats.Coins.Value = data[2] or 0

i just tried and it wasnt working for some reason. it said and returned nil

Could you please show me your exact script?

Try making a table that contains both wins and coins.

Then you can access them by(for example) coins is first, wins is second.

local coins= data[1] – gets first index(coins)

local wins= data[2] – gets second index(wins)

local DSS = game:GetService('DataStoreService')
local myDataStore = DSS:GetDataStore('myDatastore')

game.Players.PlayerAdded:Connect(function(plr)
	--gets the int value which is wins 
	local wins = plr:WaitForChild('leaderstats').Wins
	
	local coins = plr:WaitForChild('leaderstats').Coins
	--create an empty variable to get the value of wins from the player
	local data
	local data2 
	
	local success, error = pcall(function()
		data = myDataStore:GetAsync(plr.UserId.."_wins", plr.leaderstats.Wins.Value)
		data2 = myDataStore:GetAsync(plr.UserId.."_coins", plr.leaderstats.Coins.Value)
	end)
	
	if success then
		wins.Value = data 
		print("Data has been reached!")
		coins.Value = data2
		print('Coins had been reached!')
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local ToSave = {
		plr.leaderstats.Wins.Value,
		plr.leaderstats.Coins.Value;
	}
	
	--local data = plr.leaderstats.Wins.Value
	--local data2 = plr.leaderstats.Coins.Value
	
	local success, error = pcall(function()
		local data = myDataStore:GetAsync()
		plr.leaderstats.Wins.Value = ToSave[1] or 0
		plr.leaderstats.Coins.Value = ToSave[2] or 0
		--myDataStore:SetAsync(plr.UserId.."_wins", plr.leaderstats.Wins.Value)
		--myDataStore:SetAsync(plr.UserId.." _coins", plr.leaderstats.Coins.Value)
	end)
	
	if success then
		print("Saved data!")
	else
		print('Error!')
		warn(error)
	end
end)

You could combine both values into one data store thing only. Make it less complicated and easier to add on to.

Here:

local DSS = game:GetService('DataStoreService')
local myDataStore = DSS:GetDataStore('myDatastore')

game.Players.PlayerAdded:Connect(function(plr)
	--gets the int value which is wins 
	local wins = plr:WaitForChild('leaderstats').Wins
	
	local coins = plr:WaitForChild('leaderstats').Coins
	--create an empty variable to get the value of wins from the player
	local data
	
	local success, error = pcall(function()
		myDataStore:GetAsync(plr.UserId.."-data")
	end)
	
	if success then
		wins.Value = data[1]
		print("Data has been reached!")
		coins.Value = data[2]
		print('Coins had been reached!')
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local ToSave = {
		plr.leaderstats.Wins.Value,
		plr.leaderstats.Coins.Value;
	}
	
	--local data = plr.leaderstats.Wins.Value
	--local data2 = plr.leaderstats.Coins.Value
	
	local success, error = pcall(function()
		myDataStore:SetAsync(plr.UserId.."-data", ToSave)
	end)
	
	if success then
		print("Saved data!")
	else
		print('Error!')
		warn(error)
	end
end)

at line 10 its gives and error saying it attempted to index nil with a number. maybe their isnt any tables yet for data?

Wait, sorry.

in the first pcall() set data to the GetAsync

data = myDataStore:GetAsync(plr.UserId.."-data")

I

local DSS = game:GetService('DataStoreService')
local myDataStore = DSS:GetDataStore('myDatastore')

game.Players.PlayerAdded:Connect(function(plr)
	--gets the int value which is wins 
	local wins = plr:WaitForChild('leaderstats').Wins

	local coins = plr:WaitForChild('leaderstats').Coins
	--create an empty variable to get the value of wins from the player
	local data

	local PlayerId = plr.UserId

	local success, Error = pcall(function()
		data = myDataStore:GetAsync(PlayerId)
	end)

	if success then
		wins.Value = data[1] 
		print("Data has been reached!")
		coins.Value = data[2]
		print('Coins had been reached!')
	else
		print(Error)
	end
end)

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

	local PlayerId = plr.UserId
	--local data = plr.leaderstats.Wins.Value
	--local data2 = plr.leaderstats.Coins.Value

	local success, Error = pcall(function()
		myDataStore:SetAsync(PlayerId, {plr.leaderstats.Wins.Value, plr.leaderstats.Coins.Value})
	end)

	if success then
		print("Saved data!")
	else
		print('Error!')
		warn(Error)
	end
end)

that does not work. no errors it just doesnt save

it doesnt work but their isnt any tables for data. so i am calling an index on something that has no existence

can i see the leaderstats script

Try it in Roblox Player, sometimes it doesn’t work in Studio

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = plr
	
	
	local wins = Instance.new('IntValue')
	wins.Parent = leaderstats
	wins.Name = 'Wins'
	
	
	local coins = Instance.new('IntValue', leaderstats)
	coins.Name = 'Coins'
end) 

ok just tested it. it works in the player but not studio? I am confused why

This happens because in Studio, servers usually shut down before they can detect the player leaving.

Use game:BindToClose() to detect the game shutting down.