I was wondering if there was a more efficient way to load my datastore data.

I am guessing it would require some sort of

for i, v in pairs(PlayerData) do

Or something like that, but here's the code (sorry for all the lines of code, it's pretty much the same code just repeated x6 times):

local DataStoreService = game:GetService("DataStoreService")
local DataStore= DataStoreService:GetDataStore("PlayerData")
local PlayerData= {}

function LoadData(Player)
	repeat wait() until game.Players:FindFirstChild(Player.Name)	
	local Player = game.Players:FindFirstChild(Player.Name)

	if Player then		
		local Data = DataStore:GetAsync(Player.userId) or {}
		Data .Cash= Data .Cash or 0
		
		Data.ItemData1  = Data.ItemData1 or 0
		Data.ItemData2 = Data.ItemData2 or 0
		Data.ItemData3 = Data.ItemData3 or 0
		Data.ItemData4 = Data.ItemData4 or 0
		Data.ItemData5 = Data.ItemData5 or 0
		Data.ItemData6 = Data.ItemData6 or 0
		Data.ObjectId1 = Data.ObjectId1 or 0
		Data.ObjectId2 = Data.ObjectId2 or 0
		Data.ObjectId3 = Data.ObjectId3 or 0
		Data.ObjectId4 = Data.ObjectId4 or 0
		Data.ObjectId5 = Data.ObjectId5 or 0
		Data.ObjectId6 = Data.ObjectId6 or 0
				
		PlayerData[Player.userId] = Data 
		
		Currency = Instance.new("IntValue", Player)
		Cash= Instance.new("IntValue", Currency)
		ItemData = Instance.new("IntValue", Player)
		
		ItemData1 = Instance.new("StringValue", ItemData)
		ItemData2 = Instance.new("StringValue", ItemData)
		ItemData3 = Instance.new("StringValue", ItemData)
		ItemData4 = Instance.new("StringValue", ItemData)
		ItemData5 = Instance.new("StringValue", ItemData)
		ItemData6 = Instance.new("StringValue", ItemData)
		ObjectId1 = Instance.new("IntValue", ItemData1)
		ObjectId2 = Instance.new("IntValue", ItemData2)
		ObjectId3 = Instance.new("IntValue", ItemData3)
		ObjectId4 = Instance.new("IntValue", ItemData4)
		ObjectId5 = Instance.new("IntValue", ItemData5)
		ObjectId6 = Instance.new("IntValue", ItemData6)
		SetDataNames() --Changes all the names of the values, eg ItemData1.Name = "ItemData1", just didn't add it here because it would be so much lines of code/spammy.
		
		Cash.Value = Data .Cash
		
		ItemData1.Value = Data.ItemData1
		ItemData2.Value = Data.ItemData2
		ItemData3.Value = Data.ItemData3
		ItemData4.Value = Data.ItemData4
		ItemData5.Value = Data.ItemData5
		ItemData6.Value = Data.ItemData6
		ObjectId1.Value = Data.ObjectId1
		ObjectId2.Value = Data.ObjectId2
		ObjectId3.Value = Data.ObjectId3
		ObjectId4.Value = Data.ObjectId4
		ObjectId5.Value = Data.ObjectId5
		ObjectId6.Value = Data.ObjectId6
	end
end

EDIT: I mean how can I make the code shorter?

1 comment

0

for it to load "faster" you could use ServerScriptService. It runs scripts faster than anywhere else.