Changing table values into object values

Hi, I am trying to get a table that stores data and trying to convert all the data into object values using for loop.

My attempt didn’t work:

local function DataTest(player)
	local profile = Profiles[player]
	
	local PlayerFolder = Instance.new("Folder",game:GetService("ReplicatedStorage").PlayerData)
	PlayerFolder.Name = player.Name
	
	local CurrencyFolder = Instance.new("Folder",PlayerFolder)
	CurrencyFolder.Name = "Currency"
	
	local UpgradesFolder = Instance.new("Folder",PlayerFolder)
	UpgradesFolder.Name = "Upgrades"
	
	for i,v in ipairs(profile.Data.Currency) do
		local dataValue = Instance.new("IntValue")
		dataValue.Name = tostring(v)
		dataValue.Value = v
		dataValue.Parent = CurrencyFolder
	end
	
	print(profile.Data.Currency)
end

I would suggest not using ValueObjects for storing data. Use tables instead. Objects are not saveable through DataStoreService (including modules that utilize DataStoreService).

What do you really need to do with this code? Spawn objects?

I am trying to replicate the data into replicatedstorage so I can transfer data between client and server

Just use RemoteEvents and RemoteFunctions. They are much more reliable. Place a RemoteEvent under ReplicatedStorage, use FireClient(player, other data) on the server, and connect a LocalScript to that RemoteEvent with RemoteEvent.OnClientInvoke().

Player joining sample:

Server:

local RE = game:GetService("ReplicatedStorage"):WaitForChild("ClientReceive")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player) -- Send data to player who joined
	RE:FireClient(Player, 200)
end)

LocalScript:

local RE = game:GetService("ReplicatedStorage"):WaitForChild("ClientReceive")

RE.OnClientEvent:Connect(function(Points) -- Receive data from server
	print(Points)
end)
1 Like

The game I am working on has everything local so I’m not sure if I should keep using remotes. Even Collecting in my game requires remotes.

The game I am working on is inspired by: [BIG UPD!] ☀️ Grass Cutting Incremental ☀️ - Roblox

I advise not to put critical logic such as saving/loading data on the client’s side, as exploiters essentially have easier access to it and start cheating. Put such code in the server (under ServerScriptService).

1 Like

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