Hello! My datastore script isn’t loading tools:
Data handler script:
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
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()
data = DS:SetAsync(key, data)
end)
if not success then
warn("Error while saving data: "..err)
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
Remote handler script:
local Remotes = game:GetService("ReplicatedStorage").Remotes
Remotes.Shop.OnServerEvent:Connect(function(player, tool)
local data = game:GetService("DataStoreService"):GetDataStore("Data"):GetAsync("UID_"..player.UserId)
print(data)
local s, e = pcall(function()
table.insert(data.Tools, tool)
end)
end)
Localscript that fires the shop remote:
local player = game.Players.LocalPlayer
repeat wait() until player
local tools = game.ReplicatedStorage.Tools
local tokens = player.leaderstats.Tokens
local shop = player.PlayerGui.Shop
local shopre = game.ReplicatedStorage.Remotes.Shop
local debounce = false
for i, v in pairs(script.Parent:GetChildren()) do
if not debounce then
debounce = true
if v:IsA("Frame") then
local button = v.Image
button.MouseButton1Click:Connect(function()
local price = v:GetAttribute("Price")
if not player.Backpack:FindFirstChild(v) then
if tokens.Value >= price then
if tools[v.Name] then
tools[v.Name]:Clone().Parent = player.Backpack
tools[v.Name]:Clone().Parent = player.StarterGear
shopre:FireServer(v.Name)
shop.Enabled = false
end
else
print(player.Name..' doesn\'t have enough for '..v.Name)
shop.Enabled = false
end
else
print(player.Name..' already has '..v.Name)
shop.Enabled = false
end
end)
end
end
task.wait(1)
debounce = false
end
I’m confused as to what your issue is. Just reply with the number that represents your situation, and add a little description afterwards so you get over the word limit.
You don’t load in with the item even though it shows it in the explorer
The item is being purchased from the shop correctly, but when you rejoin, it’s no longer there.
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)