Hello,
I want to make a music gui where you can add favorite songs to a ScrollingFrame. It would be pretty useless if the favorites aren’t saved, but i don’t know how to use data stores properly to save it.
My questions are:
1. How can i save multiple values (Using data stores) without knowing how much values i need to save?
2. How can i create a TextButton for each saved value after i joined?
3. Is it even possible to save gui objects without serialization?
4. How can i get seperated values from the data store? For example: I have 2 values saved. One value who contains the name of the song, and another one who contains the song id. How can i get this values seperated back?
So far i tried to understand how data stores work by reading the Data Stores article.
And i was looking for similar topics like this or this.
I also watched a few youtube videos like this:
After that my data store Script looked like this:
local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:connect(function(player)
local Datastore = DataStoreService:GetDataStore(player.UserId.." FavsTest")
local success, Datastore = pcall(function()
local Data = Datastore:GetAsync("favs")
for i = 1, #Data do
local nval = Instance.new("StringValue")
nval.Name = "Name"
nval.Value = Data.Value
local idval = Instance.new("StringValue")
idval.Name = "ID"
idval.Parent = nval
idval.Value = Data.Value
end
end)
if not success then
print(Datastore)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local Datastore = DataStoreService:GetDataStore(player.UserId.." FavsTest")
local Saving = player:FindFirstChild("favs"):GetChildren()
local success, errorMessage = pcall(function()
for i = 1, #Saving do
Datastore:SetAsync("favs", Saving[i].Name, Saving[i].Value)
end
end)
if not success then
print(errorMessage)
end
end)
But it obviously doesn’t work…
And it prints this error:
Unable to cast to Array - Server
This is the LocalScript that creates the TextButtons in the ScrollingFrame:
local frame = script.Parent.Parent.Parent.ScrollingFrame
local temp = frame.Template
local favs = frame.favs
local count
script.Parent.MouseButton1Click:Connect(function()
count = #favs:GetChildren()
local favb = Instance.new("TextButton")
favb.Name = "fav"..count
favb.Parent = favs
favb.Position = temp.Position
favb.Size = temp.Size
favb.BackgroundTransparency = 0.5
favb.TextColor3 = Color3.new(1, 1, 1)
favb.TextScaled = true
favb.Font = Enum.Font.Fondamento
favb.Text = script.Parent.Parent.Parent.mname.Text
local uic = Instance.new("UICorner")
uic.Parent = favb
local val = Instance.new("StringValue")
val.Name = "id"
val.Parent = favb
val.Value = workspace:WaitForChild("MusicPlayerSound").SoundId
local lsc = temp.LocalScript:Clone()
lsc.Parent = favb
local name = script.Parent.Parent.Parent.mname.Text
local id = workspace:WaitForChild("MusicPlayerSound").SoundId
game.ReplicatedStorage.MusicPlayerEvents.AddFav:FireServer(id, name)
end)
And this is the Script that creates the values:
game.ReplicatedStorage.MusicPlayerEvents.AddFav.OnServerEvent:Connect(function(plr, id, name)
local nval = Instance.new("StringValue")
nval.Name = "Name"
nval.Parent = plr.favs
nval.Value = name
local idval = Instance.new("StringValue")
idval.Name = "ID"
idval.Parent = nval
idval.Value = id
end)
Here a short video so you can see how the values are stored:
I’m sorry if something is misleading or misspelled.
It’s my first post here and i hope you guys can help me.
Thank You