Trouble to load datas from the player

Hi so here is my script to save value from the player Data’s folder here is the module script :

--!strict

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)

Knit.OnStart():await()

local DataService = Knit.GetService("DataService")


local function getData(player: Player)	
	local Success, Profile = DataService:getProfile(player):await() -- await() returns 2 values bool --success/unsuccess--, Value
	--local Profile = DataService:getProfile(player) -- if your not using await()

	if Success then
		local Data = Profile.Data

		return Data
	end
end

local LoadData = {}

function LoadData.Update(Player : Player)
	local DataF = Player:FindFirstChild("Data") or Instance.new("Folder", Player)
	DataF.Name = "Data"
	local Data = getData(Player)
	
	print(string.format("Datas of player : %s are :", Player.Name))
	print(Data)
	
	for i, data in Data.Player do
		print(i)
		local value = data.Value
		local Type = data.Type
		local Parent = data.Parent
		
		local Value = DataF:FindFirstChild(i) or Instance.new(Type, DataF)
		Value.Name = i
		if value ~= nil then
			Value.Value = value 
		end
		if Parent ~= nil then
			Value.Parent = DataF:FindFirstChild(Parent)
		end
	end
end

function LoadData.Save(Player : Player)
	local DataF = Player:FindFirstChild("Data") or Instance.new("Folder", Player)
	DataF.Name = "Data"
	local Data = getData(Player)

	
	for i, data in DataF:GetDescendants() do
		print(string.format("Saved : %s , for player : %s", data.Name, Player.Name))
		
		if data:IsA("Folder") then continue end
		
		local Type = nil
		if data:IsA("StringValue") then
			Type = "StringValue"
		elseif data:IsA("BoolValue") then
			Type = "BoolValue"
		elseif data:IsA("NumberValue") then
			Type = "NumberValue"
		end
		
		if data.Parent == DataF then
			Data.Player[data] = {Value = data.Value, Type = Type}
		elseif data.Parent:IsA("Folder") then
			Data.Player[data] = {Value = data.Value, Type = Type, Parent = data.Parent.Name}
		end
		
	end
	
	print("Saved!")
end

return LoadData

And here is the DataTemplate:

local module = {
	["Player"] = {
		["DevilFruit"] ={Value = "flame-flame", Type = "StringValue"};
		["Melee"] ={Value = "Fist", Type = "StringValue"};
		["Level"] ={Value = "1", Type = "NumberValue"};
		["EXP"] ={Value = "0", Type = "NumberValue"};
		
		["Melli"] ={Value = "100", Type = "NumberValue"};
		
		["Inventory"] ={Value = nil, Type = "Folder"};
		["Sword"] ={Value = true, Type = "BoolValue", Parent = "Inventory"};
		
	}
	
}

return module

The thing is that when i launch the game back after a save everything work fine beside the name of the value that is like that : Instance (Level) or Instance (DevilFruit) instead of just : Level or DevilFruit.Thanks for any help!

image

Ok so i just figured it out by replacing data by data.Name Like that :

--!strict

local Knit = require(game:GetService("ReplicatedStorage").Packages.Knit)

Knit.OnStart():await()

local DataService = Knit.GetService("DataService")


local function getData(player: Player)	
	local Success, Profile = DataService:getProfile(player):await() -- await() returns 2 values bool --success/unsuccess--, Value
	--local Profile = DataService:getProfile(player) -- if your not using await()

	if Success then
		local Data = Profile.Data

		return Data
	end
end

local LoadData = {}

function LoadData.Update(Player : Player)
	local DataF = Player:FindFirstChild("Data") or Instance.new("Folder", Player)
	DataF.Name = "Data"
	local Data = getData(Player)
	
	print(string.format("Datas of player : %s are :", Player.Name))
	print(Data)
	
	for i, data in Data.Player do
		print(i)
		local value = data.Value
		local Type = data.Type
		local Parent = data.Parent
		
		local Value = DataF:FindFirstChild(i) or Instance.new(Type, DataF)
		Value.Name = i
		if value ~= nil then
			Value.Value = value 
		end
		if Parent ~= nil then
			Value.Parent = DataF:FindFirstChild(Parent)
		end
	end
end

function LoadData.Save(Player : Player)
	local DataF = Player:FindFirstChild("Data") or Instance.new("Folder", Player)
	DataF.Name = "Data"
	local Data = getData(Player)

	
	for i, data in DataF:GetDescendants() do
		print(string.format("Saved : %s , for player : %s", data.Name, Player.Name))
		
		if data:IsA("Folder") then continue end
		
		if data.Parent == DataF then
			Data.Player[data.Name] = {Value = data.Value, Type = data.ClassName}
		elseif data.Parent:IsA("Folder") then
			Data.Player[data.Name] = {Value = data.Value, Type = data.ClassName, Parent = data.Parent.Name}
		end
		
	end
	
	print("Saved!")
end

return LoadData

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.