I’m new to DataStores and am saving items players have bought with their in-game points via one. However, I put in a vending machine that gives the player an item for free, but I DON’T want it to be saved by the DataStore- players have to pick it up every time. It doesn’t get saved, which is the good part, however it spams my console with DataStore save requests.
How do I fix this?
Here’s my DataStore’s code:
local dss = game:GetService("DataStoreService")
local SStorage = game:GetService("ServerStorage")
local DS = dss:GetDataStore("InventorySave")
local wepfolder = SStorage.Weapons
local cansave = true
game.Players.PlayerAdded:Connect(function(player)
local inventory = Instance.new("Folder", SStorage.Inventories)
inventory.Name = player.Name
local keycode = "keycode: "..player.UserId
local data
if DS:GetAsync(keycode) then
data = DS:GetAsync(keycode)
if #data > 0 then
for i,v in pairs(wepfolder:GetChildren()) do
for q,tool in pairs(data) do
if tool == v.Name then
v:clone().Parent = inventory
end
end
end
print('found some items bought by: '..keycode..'['..player.Name..']')
else
print('no stuff saved: '..player.Name)
end
else
print('new player:'..keycode..'['..player.Name..']')
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local inventoryfolder = SStorage.Inventories:FindFirstChild(player.Name)
pcall(function()
if inventoryfolder then
game.Debris:AddItem(inventoryfolder, 2)
end
end)
local keycode = "keycode: "..player.UserId
local weps = {}
local pos = 1
for i,v in pairs(inventoryfolder:GetChildren()) do
if v:IsA("Tool") then
table.insert(weps, pos, v.Name)
pos = pos + 1
end
end
if #weps > 0 then
local succ, err = pcall(function()
DS:SetAsync(keycode, weps)
end)
if succ then
print('saved inventory for: '..player.Name)
else
print(tostring(err))
end
end
end)
function save(player)
local inventoryfolder = SStorage.Inventories:FindFirstChild(player.Name)
if not inventoryfolder then return end
local keycode = "keycode: "..player.UserId
local weps = {}
local pos = 1
for i,v in pairs(inventoryfolder:GetChildren()) do
if v:IsA("Tool") then
table.insert(weps, pos, v.Name)
print('added weapon: '..v.Name..' to position: '..pos)
pos = pos + 1
end
end
if #weps > 0 then
local succ, err = pcall(function()
DS:SetAsync(keycode, weps)
end)
if succ then
print('saved inventory for: '..player.Name)
else
print(tostring(err))
end
end
end
while true do
wait(60)
for i,v in pairs(game.Players:GetPlayers()) do
if cansave == true then
save(v)
cansave =false
delay(10, function()
cansave = true
end)
end
end
end
Now I’m not sure why it would be spamming save requests, as (to my knowledge), this shouldn’t save the player’s Backpack upon a new item being added to it.
Providing code examples with your explanations would be awesome, I learn best by example!