I am trying to save multiple values IF one of the values is true. So for example, if the player has a server in the game, the OwnsServer value would be true and then the player’s data would be saved.
Here is what I have so far:
local datastoreservice = game:GetService("DataStoreService")
local serverStorage = datastoreservice:GetDataStore("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local storage = Instance.new("Folder")
storage.Name = "Storage"
storage.Parent = player
local ownsServer = Instance.new("BoolValue")
ownsServer.Name = "OwnsServer"
ownsServer.Parent = storage
local serverName = Instance.new("StringValue")
serverName.Name = "ServerName"
serverName.Parent = storage
local owner = Instance.new("StringValue")
owner.Parent = storage
owner.Name = "Owner"
local data
local success, err = pcall(function()
return serverStorage:GetAsync(player.UserId.."-ServerStorage")
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
if player.Storage.OwnsServer.Value == true then
local success, err = pcall(function()
local tableToSave = {}
table.insert(tableToSave, player.Storage.OwnsServer.Value)
table.insert(tableToSave, player.Storage.ServerName.Value)
table.insert(tableToSave, player.Storage.Owner.Value)
if player.Storage.OwnsServer.Value == true then
serverStorage:SetAsync(tableToSave, player.UserId)
end
end)
if success then
print("Successfully saved data")
else
warn(err)
end
else
print(player.Name.." doesn't own a server!")
end
end)
Can anybody help? Saving I believe works but I don’t know how to get the data.
Can you provide a link? I have been looking for a while and couldn’t seem to find suitable tutorials that fit my situation. It’s more about getting the multiple values of the data rather than saving it.
Hi there,
For my understanding… you want get data right? . All you need to do is to loop all children inside storage(folder) and check if their name is same to the data’s name then change the value to the data’s value. Hope you understand what im trying to say.feel free to ask anything if you dont understand
Not very sure what you mean by this? I am looping through the folder values and changing the folder values to the data value? How could I do that with the data values?
local success, err = pcall(function()
data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
for _, items in pairs(storage:GetChildren()) do
if items.Name == data.Name then -- i dont know what im doing lol
print("test")
end
end
end)
local success, err = pcall(function()
data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
for i, items in pairs(data) do
Player.Storage[i] = item-- i dont know what im doing lol
print("test")
end
end
end)
local datastoreservice = game:GetService("DataStoreService")
local serverStorage = datastoreservice:GetDataStore("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local storage = Instance.new("Folder")
storage.Name = "Storage"
storage.Parent = player
local ownsServer = Instance.new("BoolValue")
ownsServer.Name = "OwnsServer"
ownsServer.Parent = storage
local serverName = Instance.new("StringValue")
serverName.Name = "ServerName"
serverName.Parent = storage
local owner = Instance.new("StringValue")
owner.Parent = storage
owner.Name = "Owner"
local data
local success, err = pcall(function()
data = serverStorage:GetAsync(player.UserId.."-ServerStorage")
for i, item in pairs(data) do
player.Storage[i] = item
print(i)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
if player.Storage.OwnsServer.Value == true then
local success, err = pcall(function()
local tableToSave = {}
table.insert(tableToSave, player.Storage.OwnsServer.Value)
table.insert(tableToSave, player.Storage.ServerName.Value)
table.insert(tableToSave, player.Storage.Owner.Value)
if player.Storage.OwnsServer.Value == true then
serverStorage:SetAsync(player.UserId.."-ServerStorage", tableToSave)
end
end)
if success then
print("Successfully saved data")
else
warn(err)
end
else
print(player.Name.." doesn't own a server!")
end
end)
I changed print(i) to print(data) and I got Instance. I think this is because I am saving multiple values and also trying to get multiple files, so it can’t print all of them?