Hi, is it possible to use the Datastore to save the models that are in a folder attached to the local player? If so, how?
You cannot directly save models in a datastore but you can do something like this:
local dataStoreService = game:GetService("DataStoreService")
local modelsThing = dataStoreService:GetDataStore("Models")
local modelsFolder = game:GetService("ServerStorage"):WaitForChild("ModelsFolder")
game.Players.PlayerAdded:Connect(function(player)
local models = modelsThing:GetAsync(player.UserId)
if models ~= nil then
for _,v in pairs(modelsFolder:GetChildren()) do
if string.find(models, v.Name) then
print("here is a matching model. lets do something with it!")
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
-- lets just get all of the models in their backpack
local checking = player.Backpack
local foundModels = modelsThing:GetAsync(player.UserId) or ""
for _,v in pairs(checking:GetChildren) do
if v:IsA("Model") then
foundModels = foundModels.."$"..v.Name.."$"
-- seperate the names by $ so they don't get all mixed up!
end
end
modelsThing:SetAsync(player.UserId, foundModels)
end)
You can always create a custom folder in the player a loop through that. This works by just matching the names to real models so you’d have to have another folder that had all the models in it that it can match.
Other than this you could just save a huge string with code in it on how to reconstruct the models with all of the parts and stuff in it.
I have this but like… I don’t know what to do with models…?
game.Players.PlayerRemoving:Connect(function(player)
local data = {}
data.equppedTrap = player.equippedTrap.Value
data.equippedSkin = player.equippedSkin.Value
data.Skins = {}
data.Traps = {}
for i, v in pairs(player.SkinInventory:GetChildren()) do
table.insert(data.Skins, v.Name)
print(v.Name)
end
for i, v in pairs(player.TrapInventory:GetChildren()) do
table.insert(data.Traps, v.Name)
print(v.Name)
end
local sucess, errorMsg = pcall(function()
DataStore:SetAsync(player.UserId, data)
end)
if sucess then
print("Successfully saved")
end
end)
Do you have the actual datastore or the part conversion?
I have a datastore:
game.Players.PlayerAdded:Connect(function(player)
local SkinInventory = Instance.new("Folder")
SkinInventory.Name = "SkinInventory"
SkinInventory.Parent = player
local equippedSkin = Instance.new("StringValue")
equippedSkin.Name = "equippedSkin"
equippedSkin.Parent = player
local inMenu = Instance.new("BoolValue")
inMenu.Name = "inMenu"
inMenu.Parent = player
local data
local success, errorMsg = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if data ~= nil then
if data.equippedTrap then
equippedTrap.Value = data.equippedTrap
end
if data.equippedSkin then
equippedSkin.Value = data.equippedSkin
end
if data.Skins then
for i, v in pairs(data.Skins) do
local val = Instance.new("StringValue")
val.Name = v
val.Parent = SkinInventory
end
end
end
game.ReplicatedStorage.SendData:FireClient(player, data)
end)
end)
So what exactly does this do? Does it like store the name of the part or… ?
Currently, it takes the skin you have purchased and create a stringValue of the skin’s name.
Right but I think you should replace it because you’re indexing values in the datastore that don’t seem to exist.
Replace what? the stringvalues?
The entire script because the way you refer to he datastore.equippedSkin would be in a table which you cannot store in a datastore.
The equipped skin is something differe and I don’t want to touch that. The skin’s the player has are all stored in a a folder.