Is something wrong with this datastore script?

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

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local CashV= Instance.new("IntValue", Leaderstats)
	CashV.Name = "Cash"
	CashV.Value = 0
	local LevelV= Instance.new("IntValue", Leaderstats)
	LevelV.Name = "Level"
	LevelV.Value = 1
	local WinsV= Instance.new("IntValue", Leaderstats)
	WinsV.Name = "Wins"
	WinsV.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		CashV.Value = Data.Cash
		LevelV.Value = Data.Level
		WinsV.Value = Data.Wins
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Cash"] = Player.leaderstats.Cash.Value;
		["Level"] = Player.leaderstats.Level.Value;
		["Wins"] = Player.leaderstats.Wins.Value;
	})
end)

Seems about right. If the code above doesn’t work, then:

  1. Enable HTTP and API requests in the studio settings, since those might prevent you from getting data directly in studio.
  2. Try playing the game outside of studio. Whenever you stop playtesting, your player leaves the local server and the server gets shutdown immediately as well.
1 Like