I’m giving you scenarios so I can better understand what your issue is. It’s not very clear in your post. How do you know your script isn’t loading tools? Where are you seeing what’s being loaded? After which point is it supposed to be saved? What are your errors? What have you already tried to fix it?
You should include all of this and more in your initial post.
It’s a server script in serverscriptservice. Here’s the code.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Data")
local function GenerateUID(player)
return "UID_"..player.UserId
end
function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tokens = Instance.new("IntValue")
tokens.Name = "Tokens"
tokens.Parent = leaderstats
local data
local key = GenerateUID(player)
local success, err = pcall(function()
data = DS:GetAsync(key)
end)
if not success then
player:Kick("\nDataHandler\n\nCouldn't fetch data!")
else
print("Successfully got data")
for i,v in pairs(data.Tools) do
print(v)
if game.ReplicatedStorage.Tools:FindFirstChild(v) then
local clone = game.ReplicatedStorage.Tools[v]:Clone()
clone.Parent = player.Backpack
clone.Parent = player.StarterGear
end
end
end
end
function onPlayerRemoving(player)
local data = {
Tokens = player.leaderstats.Tokens.Value,
Tools = {
--starter sword
}
}
local key = GenerateUID(player)
local success, err = pcall(function()
DS:SetAsync(key, data)
end)
if not success then
warn("Error while saving data: "..err)
else
print("Successfully saved data")
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
You need to either store some sort of global or shared table, or just Instance:GetChildren() the player’s folder of tools. You definitely should not use the actual tools that the player will be using as the reference, though. You could save the table every time the player makes a purchase.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Data")
local function GenerateUID(player)
return "UID_"..player.UserId
end
function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tokens = Instance.new("IntValue")
tokens.Name = "Tokens"
tokens.Parent = leaderstats
local folder = Instance.new("Folder")
folder.Name = player.Name
folder.Parent = game.ReplicatedStorage.PlayerTools
local data
local key = GenerateUID(player)
local success, err = pcall(function()
data = DS:GetAsync(key)
end)
if not success then
player:Kick("\nDataHandler\n\nCouldn't fetch data!")
else
tokens.Value = data.Tokens
print("Successfully got data")
for i,v in pairs(data.Tools) do
print(v)
if game.ReplicatedStorage.Tools:FindFirstChild(v) then
local clone = game.ReplicatedStorage.Tools[v]:Clone()
clone.Parent = player.Backpack
clone.Parent = player.StarterGear
end
end
end
end
function onPlayerRemoving(player)
local data = {
Tokens = player.leaderstats.Tokens.Value,
Tools = game.ReplicatedStorage.PlayerTools[player.Name]:GetChildren()
}
print(data.Tools)
local key = GenerateUID(player)
local success, err = pcall(function()
DS:SetAsync(key, data)
end)
if not success then
warn("Error while saving data: "..err)
else
print("Successfully saved data")
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
I miscommunicated. You can’t save instances to a datastore. If you’re going to go the instance route, you need to be storing string values of some sort, and then loading the object that corresponds to that value.
I highly suggest doing the alternative of referencing a table of stored strings that will be used to fetch the player’s tools, though.
For my games, I create a table for each player that keeps their player info in _G. Only use that if you’re comfortable with it, though. Many people will tell you not to use it, because you can easily overwrite it if you’re not careful. Otherwise, you need to find some other way to share the table between scripts. Saving after the player purchases an item, and then loading that table and saving again after they leave would work, but it’s less flexible. There are many ways to do this, but they all have their disadvantages, unfortunately.
I think so. Try it out. There may be another option too.
I, personally, never really liked the idea of using instances for this sort of thing, but other people do it. It’s a lot easier, but it’s also messy in my opinion. I just wanted to give you every option I could think of.
Edit: I will say, though, I recommend keeping that folder in each player’s Player object. You won’t have to worry about removing it after they leave, and you certainly don’t want every player’s tools being replicated to each client.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Data")
local function GenerateUID(player)
return "UID_"..player.UserId
end
function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local tokens = Instance.new("IntValue")
tokens.Name = "Tokens"
tokens.Parent = leaderstats
local folder = Instance.new("Folder")
folder.Name = "Tools"
folder.Parent = player
local data
local key = GenerateUID(player)
local success, err = pcall(function()
data = DS:GetAsync(key)
end)
if success then
tokens.Value = data.Tokens
print("Successfully got data")
print(data.Tools)
for i,v in pairs(data.Tools) do
print(v)
if game.ReplicatedStorage.Tools:FindFirstChild(v) then
local clone = game.ReplicatedStorage.Tools:FindFirstChild(v):Clone()
print(clone)
clone.Parent = player.Backpack
clone.Parent = player.StarterGear
clone.Parent = player.Tools
end
end
else
player:Kick("\nDataHandler\n\nCouldn't fetch data!")
end
end
function onPlayerRemoving(player)
local data = {
Tokens = player.leaderstats.Tokens.Value,
Tools = {}
}
for _,v in pairs(player.Tools:GetChildren()) do
table.insert(data.Tools, v.Name)
print(v)
end
local key = GenerateUID(player)
local success, err = pcall(function()
DS:SetAsync(key, data)
end)
if success then
print("Successfully saved data")
else
warn("Error while saving data: "..err)
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)