Alright, so I have a Leaderboard script and it works fine but the problem is, that it's not saving my data or other players data and another problem I'm having is the tools, they're not saving when the player leaves the game after they bought it from my shop gui, I don't know why it's not working properly. please help.

The error I keep getting in my Leaderboard script is this:

ServerScriptService.Leaderboard:17: attempt to index global 'data' (a number value)

My Leaderboard script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
	local leaderboard = Instance.new("Folder",player)
	leaderboard.Name = "Leaderboard"

	local cash = Instance.new("IntValue",leaderboard)
	cash.Name = "Cash"
	cash.Value = 50
	
	local recieved,failed = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
	
	if recieved then
		cash.Value = data[1]
		print("Data was received")
	else
		print("Data was not received")	
	end
	
	while true do wait(30)
		dataStore:SetAsync(player.UserId, cash.Value)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderboard = player.Leaderboard
	local cash = leaderboard.Cash
	
	local saved,failed = pcall(function()
		dataStore:SetAsync(player.UserId, cash.Value)
	end)
	
	if saved then
		print("Data was saved")
	else
		print("Data was not saved")
	end
end)

My Inventory Saving script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
	local leaderboard = Instance.new("Folder",player)
	leaderboard.Name = "Leaderboard"

	local cash = Instance.new("IntValue",leaderboard)
	cash.Name = "Cash"
	cash.Value = 50
	
	local recieved,failed = pcall(function()
		data = dataStore:GetAsync(player.UserId)
	end)
	
	if recieved then
		cash.Value = data[1]
		print("Data was received")
	else
		print("Data was not received")	
	end
	
	while true do wait(30)
		dataStore:SetAsync(player.UserId, cash.Value)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderboard = player.Leaderboard
	local cash = leaderboard.Cash
	
	local saved,failed = pcall(function()
		dataStore:SetAsync(player.UserId, cash.Value)
	end)
	
	if saved then
		print("Data was saved")
	else
		print("Data was not saved")
	end
end)

Any help would be appreciated.

2 comments

0

Have you tried to remove the [1] on the data saver personally I never seen that been used?

0

@thecoolboycool2 That [1] depends on how data is being saved, but I think he needs to remove it