I'm pretty new to scripting so I decided to use an AlvinBlox youtube tutorial to make my own ShopGui. I got to the part of the tutorial where I would make a DataStore that saves my tools that I bought using the shop. However, when I buy an item, I get the item, but once I rejoin it doesn't save. I'm not sure what is wrong with the script or how to fix it. Some help would be greatly appreciated, thanks. API services is on too. Script:
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStorage")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 750
cash.Parent = folder
local data
local success, errorMessage = pcall(function()
data = DataStore:GetAsync(plr.UserId)
end)
if data ~= nil then
for _, toolName in pairs(data) do
local tool = game.ReplicatedStorage:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = plr.Backpack
local newTool = tool:Clone()
newTool.Parent = plr.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId, toolsTable)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId, toolsTable)
end)
end
end)

3 comments
you wrote uderid instead of userid at line 16
and you need to enable API services
I changed it to UserId, turned on API services and still doesnt work