Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datastore won't work, and I have no idea why at all? [Solved]

Here is the whole code which is in one script:

local DataStoreService = game:GetService("DataStoreService")
local Stats = DataStoreService:GetDataStore("Stats")

game.Players.PlayerAdded:connect(function(player)
	
	-- get parts for system
	local info = Instance.new("StringValue", game.Workspace.Player_Information)
	info.Name = player.Name
	
	-- set up other parts
	Instance.new("StringValue",info).Name = "Inventory"
	Instance.new("StringValue", info.Inventory).Name = "Gloves"
	Instance.new("IntValue", info).Name = "Box_Bux"
	Instance.new("StringValue", info).Name = "Wearing"
	Instance.new("StringValue", info.Wearing).Name = "Gloves"
	
	
	local can_save = Instance.new("BoolValue", info)
	can_save.Name = "Can_Save"
	can_save.Value = true
	
	local stats = game.Lighting.Stats:Clone()
	stats.Parent = info
	
	-- load data table
	local success, message = pcall(function()
		stat_table = Stats:GetAsync(player.UserId)
	end)
	
	
	if success then
		if stat_table ~= nil then
			
			print(stat_table["Gloves_Inventory"])
			print(stat_table["Fitness"])
			
			stats.Strength.Value = stat_table.Strength
			
			stats.Fitness.Value = stat_table.Fitness
			
			stats.Endurance.Value = stat_table.Endurance
			
			stats.Speed.Value = stat_table.Speed
			
			if stat_table.Box_Bux ~= nil then
				print("loaded properly")
				
				info.Box_Bux.Value = stat_table.Box_Bux
				
				-- load gloves in inventory
				local gloves_in_inv = stat_table.Gloves_Inventory
				local rounds = string.len(gloves_in_inv)/4
				for i = 1, rounds do
					Instance.new("StringValue", info.Inventory.Gloves).Name = string.sub(gloves_in_inv, 1 + ((i - 1)*4), 4 + ((i - 1)*4))
				end
					
				Instance.new("StringValue", info.Wearing.Gloves).Name = stat_table.Glove_Wearing
				
			else
				print("dis route")
				info.Box_Bux.Value = 0
				Instance.new("StringValue", info.Wearing.Gloves).Name = "G001"
				Instance.new("StringValue", info.Inventory.Gloves).Name = "G001"
			end
		
		else
			print("dis 1")
			stats.Strength.Value = 0
			
			stats.Fitness.Value = 0
			
			stats.Endurance.Value = 0
			
			stats.Speed.Value = 0
			
			info.Box_Bux.Value = 0
			Instance.new("StringValue", info.Wearing.Gloves).Name = "G001"
			Instance.new("StringValue", info.Inventory.Gloves).Name = "G001"
		end
	else
		can_save.Value = false
		player:Kick("Your data hasn't loaded correctly, but you haven't lost any data! Please simply rejoin the game!")
	end	
end)

game.Players.PlayerRemoving:connect(function(player)
	local player_folder = game.Workspace.Player_Information:FindFirstChild(player.Name)
	
	if player_folder.Can_Save.Value == true then
		print("saving")		
		
		-- strength	
		local strength = player_folder.Stats.Strength.Value
		
		-- fitness
		local fitness = player_folder.Stats.Fitness.Value
		
		-- endurance
		local endurance = player_folder.Stats.Endurance.Value
		
		-- speed
		local speed = player_folder.Stats.Speed.Value
		
		-- gloves in inventory
		local gloves_inv = player_folder.Inventory.Gloves:GetChildren()
		local gloves_owned = {}
		for i = 1, #gloves_inv do
			gloves_owned[i] = gloves_inv[i].Name
		end
		
		-- gloves wearing
		local gloves_wear = player_folder.Wearing.Gloves:GetChildren()
		local gloves_wearing = {}
		for i = 1, #gloves_inv do
			gloves_wearing[i] = gloves_wear[i].Name
		end
		
		local data_table = {
			["Strength"] = strength,
			["Fitness"] = fitness,
			["Endurance"] = endurance,
			["Speed"] = speed,
			["Gloves_Inventory"] = table.concat(gloves_owned, ""),
			["Glove_Wearing"] = table.concat(gloves_wearing, ""),
			["Box_Bux"] = player_folder.Box_Bux.Value
		}
		
		print(data_table["Gloves_Inventory"])
		print(data_table["Fitness"])
		
		Stats:SetAsync(player.UserId, data_table)
	end
	
	game.Workspace.Player_Information:FindFirstChild(player.Name):Remove()
end)

Essentially the code is saving player stats, and clothing etc which is all held in objects in the workspace in a player file. (a folder). I don't think there's anything wrong particularly with what I'm saving, It seems to be how it is saving. When I play the game it's so inconstant with saving, when I play on my own, and then leave, it just doesn't save at all. It saved in 2 player test mode however, so I'm not sure what the issue is. I also added in a script with the bind to close function and a wait of 10, however this appeared to change nothing. I'm not sure how to fix this as there are no errors, and people's data gets seemingly randomly reset.

5 comments

0

I have read a few other posts, and I'll attempt moving the script from workspace to ServerScriptService, and I'll try using bind to close again.

0

Do not use workspace / lighting for storage use server storage. 2nd Do not use Remove it is deprecated. 3rd do not kick the player if the data fails to load retry instead with a small delay.

0

Line 107 and 114 use #gloves_inv

0

ah! that was why it wasn't working at all! mustve been giving an error as the for loop was running too many or too few times. I will take your advice too, thank you very much, you have saved me hours of crying lol.

0

Yeah listen to Kingdom he helped write my Data Store, lets just say he is the Data Store master.

1 answer

Why waste your time making a giant script? I use a very short script that works really well:

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("ExampleDataStore")

game.Players.PlayerAdded:connect(function(player)
 local leader = Instance.new("Folder",player)
 leader.Name = "leaderstatus"
 local Bricks = Instance.new("IntValue",leader)
 Bricks.Name = "Bricks"
 Bricks.Value = ds1:GetAsync(player.UserId) or 0
 ds1:SetAsync(player.UserId, Bricks.Value)
 Bricks.Changed:connect(function()
  print("Saving Data")
  ds1:SetAsync(player.UserId, Bricks.Value)
  print(player.UserId.."'s Data of"..Bricks.Value.." "..Bricks.Name.."has been saved!")
end)
while wait(1)do
 Bricks.value = Bricks.Value+5
  end
end)

game.Players.PlayerRemoving:connect(function(player)
 print("Saving Data")
 ds1:SetAsync(player.UserId, player.leaderstats.Brick.Value)
 print(player.Name.."'s Data of"..player.leaderstats.Brick.Value.." "..player.leaderstats.Bricks.Name.." has been saved!")
end)?

This discussion is preserved as it was. New answers, comments, and votes are closed.