Edit: My game is set to private so I don't know would that make the data not stored!

My data storing script not working! The new player will set to default leaderboard value. Then I add some "Dashbux", after I leave and join the game, it changed back to default value (0)!! Is there something wrong?


-- Data Store
local DSS = game:GetService("DataStoreService")
local SS = DSS:GetDataStore("StatsStore")

-- Leader Board
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Display an 'IntValue' on leaderboard
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = 1
	level.Parent = leaderstats
	
	local dashbux = Instance.new("IntValue")
	dashbux.Name = "Dashbux"
	dashbux.Value = 0
	dashbux.Parent = leaderstats
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Value = 0
	kills.Parent = leaderstats
	
	local tag = Instance.new("StringValue")
	tag.Name = "Tag"
	tag.Value = "Player"
	tag.Parent = leaderstats
	 
	-- Get Stored Data
	local data
	local leveldata
	local dashbuxdata
	local killsdata
	local tagdata
	local success, errme = pcall(function()
		data = SS:GetAsync(player.UserId)
		leveldata = SS:GetAsync(player.UserId.."-level")
		dashbuxdata = SS:GetAsync(player.UserId.."-dashbux")
		killsdata = SS:GetAsync(player.UserId.."-kills")
		tagdata = SS:GetAsync(player.UserId.."-tag")
	end)
	
	if success then
		if data == player.UserId then
		level.Value = leveldata
		dashbux.Value = dashbuxdata
		kills.Value = killsdata
		tag.Value = tagdata
		end
	else
		print("Get Stored Level Data Error")
		warn(errme)
	end
end)

-- Save data
game.Players.PlayerRemoving:Connect(function(player)
	
	local success, err = pcall(function()
		SS:SetAsync(player.UserID,"true")
		SS:SetAsync(player.UserId.."-level",player.leaderstats.Level.Value)
		SS:SetAsync(player.UserId.."-dashbux",player.leaderstats.Dashbux.Value)
		SS:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
		SS:SetAsync(player.UserId.."-tag",player.leaderstats.Tag.Value)
	end)
	
	if success then
		print("All Data Stored")
	else
		print("Storing Data Error")
		warn(err)
	end
end)

2 comments

0

Do you have API access on? "API access must be active in order to access and test data stores in Roblox Studio. See the Articles/Data store|Data Stores article for instructions on enabling API access."

0

@xXGokyXx Ok thanks! But is my script correct?