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?
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)
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).