Hello! I've been trying to make a datastore system, but something is going wrong. Whenever I store my data, it gives a weird number. For instance, when I join the game and set up Class-D experience to 4, Security Department experience to 6, clearance level to 7, and global experience to 3 and leave, this is what it prints: 0_0_0_0_ In theory, it should print 3_7_4_6_, but it doesn't.

--[[ // Description
Manage all main Data by storing it and gathering it. Data is stored in strings and decrypted using string.split()
]]

--// Services
local DataStoreService = game:GetService("DataStoreService")
local PlayerService = game:GetService("Players")

--// Variables
local DataStoreKey = "DevelopmentStage 3/5/2022 #4" -- Change to erase or restore data (global)
local DataStore = DataStoreService:GetDataStore(DataStoreKey)

--// Logic

function FolderGatherCreate(playerModel) -- returns the folders if present, if not it creates the folders and then returns them.
	if playerModel:FindFirstChild("Data") ~= nil then -- Data folders are present
		return playerModel.Data
	else -- Data folders are not present
		--//Instancing
		local DataFolder = Instance.new("Folder")
		local TeamData = Instance.new("Folder")
		local GlobalExperience = Instance.new("IntValue")
		local KeycardClearanceLevel = Instance.new("IntValue")
		
		local ClassDFolder = Instance.new("Folder")
		local ClassDExperience = Instance.new("IntValue")
		
		local SecurityDeptFolder = Instance.new("Folder")
		local SecurityDeptExperience = Instance.new("IntValue")
		--//Properties
		DataFolder.Name = "Data"
		DataFolder.Parent  = playerModel
		
		GlobalExperience.Name = "GlobalExperience"
		GlobalExperience.Parent = DataFolder
		GlobalExperience.Value = 0
		
		KeycardClearanceLevel.Name = "ClearanceLevel"
		KeycardClearanceLevel.Parent = DataFolder
		KeycardClearanceLevel.Value = 0
		
		ClassDFolder.Name = "ClassD"
		ClassDFolder.Parent = DataFolder
		
		ClassDExperience.Name = "Experience"
		ClassDExperience.Parent = ClassDFolder
		ClassDExperience.Value = 0
		
		SecurityDeptFolder.Name = "SecurityDepartment"
		SecurityDeptFolder.Parent = DataFolder
		
		SecurityDeptExperience.Name = "Experience"
		SecurityDeptExperience.Parent = SecurityDeptFolder
		SecurityDeptExperience.Value = 0
		
		--//Misc
		return DataFolder
	end
end

PlayerService.PlayerAdded:Connect(function(playerJoined) -- Runs when a player joins the game
	--// Variables
	local playerID = playerJoined.UserId
	local DataFolder = FolderGatherCreate(playerJoined)
	
	--// Logic
	task.wait(0.1) -- Give time for Datastores to load
	
	if DataStore:GetAsync(playerID) ~= nil then
		--// Variables
		local DataGathered = DataStore:GetAsync(playerID)
		local splits = string.split(DataGathered[1],"_")
		
		--//Apply Changes
		DataFolder.GlobalExperience.Value = tonumber(splits[1])
		DataFolder.ClearanceLevel.Value = tonumber(splits[2])
		DataFolder.ClassD.Experience.Value = tonumber(splits[3])
		DataFolder.SecurityDepartment.Experience.Value = tonumber(splits[4])
	end
end)

PlayerService.PlayerRemoving:Connect(function(playerRemoved) -- Runs when a player leaves the game
	local DataFolder = FolderGatherCreate(playerRemoved)
	local StoreString = tostring(DataFolder.GlobalExperience.Value) .. "_" .. tostring(DataFolder.ClearanceLevel.Value) .. "_" .. tostring(DataFolder.ClassD.Experience.Value) .. "_" .. tostring(DataFolder.SecurityDepartment.Experience.Value) .. "_"
	print(StoreString) -- for debugging.
	DataStore:SetAsync(playerRemoved.UserId, {StoreString,true})
end)

6 comments

0

Unrelated question here, what is task.wait()?

0

Also, I think that the player is fully disconnected once your script checks for it's UserId, but that might just be me.

0

The table saved seems to not specify what is considered "first" and what is considered "second", so that might be a reason why it doesn't load correctly

0

For the task.wait() question, task.wait() is soon going to be replacing wait() because it's essentially deprecated.

0

By any chance, are you setting the values yourself in the file explorer/ property tab, from the client? Since it doesn't change anything on the server (which explains why it all reads 0) unless if you switch to the server, it naturally won't detect those changes, due to filtering enabled, and all that jazz.

0

Otherwise, I don't see any other problem.