I’m currently working on a game in which you catch a random fish, and the game will attempt to save the current fish in your inventory (backpack) via Datastore. The datastore saves the names of all the tools you have currently and then creates duplicates with those names and distributes them upon rejoining. Fish can also be sold for coins, and the datastore for coins works flawlessly.
The code works seemingly perfect in studio, but on Roblox it just continues to wipe my datastore after each rejoin no matter what piece of the code I change. I’m very inexperienced with using datastores and so I’m hoping there’s just something silly that I’m overlooking. At first I tried to make it automatic, but after that started breaking I opted for a manual button which seems to yield the same results.
local ServerScriptService = game:GetService("ServerScriptService")
local MainModule = require(ServerScriptService:WaitForChild("MainModule"))
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local FishArchive = ServerStorage:WaitForChild("FishArchive")
local SavePart = game.Workspace:WaitForChild("SavePart")
local ClickDetector = SavePart:WaitForChild("ClickDetector")
local Player_Data = DataStoreService:GetDataStore("Player_Data2")
local InventoryData = {}
local function addLeaderstats(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
return Coins
end
local function SpawnCloneFish(InventoryData,Player)
if InventoryData ~= nil then
for _,v in InventoryData do
if FishArchive:FindFirstChild(v) then
local NewFish = FishArchive:FindFirstChild(v):Clone()
for _,CoinFishValue in MainModule.CoinValuesTable do
if CoinFishValue[1] == NewFish.Name then
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = math.random(CoinFishValue[2],CoinFishValue[3])
Coins.Parent = NewFish
NewFish.Parent = Player.Backpack
end
end
end
end
end
end
local function loadData(Player)
local Coins = addLeaderstats(Player)
local InventoryKey = Player.UserId .. "_Inventory"
local CoinKey = Player.UserId .. "_Coins"
local CoinData = nil
local Success,Failure = pcall(function()
CoinData = Player_Data:GetAsync(CoinKey)
InventoryData = Player_Data:GetAsync(InventoryKey)
end)
SpawnCloneFish(InventoryData,Player)
if Success and MainModule.DebugMessages then
Coins.Value = CoinData
if MainModule.DebugMessages then
print("Data loaded for " .. Player.Name .. "," .. Player.UserId)
print(InventoryData)
end
end
while wait(2) do
print(InventoryData)
end
end
local function saveData(Player)
local leaderstats = Player:FindFirstChild("leaderstats")
local CoinKey = Player.UserId .. "_Coins"
local InventoryKey = Player.UserId .. "_Inventory"
InventoryData = {}
for _,v in Player.Backpack:GetChildren() do
table.insert(InventoryData,v.Name)
end
local Success, Failure = pcall(function()
Player_Data:SetAsync(CoinKey,leaderstats.Coins.Value)
Player_Data:SetAsync(InventoryKey,InventoryData)
end)
if Success and MainModule.DebugMessages then
print("Saved data for " .. Player.Name .. "," .. Player.UserId)
print(InventoryData)
elseif Failure and MainModule.DebugMessages then
print("Error saving data for " .. Player.Name)
warn(Failure)
end
end
Players.PlayerAdded:Connect(function(Player)
loadData(Player)
end)
ClickDetector.MouseClick:Connect(function(Player)
saveData(Player)
end)
Here is my current rendition of the script, what could I be doing wrong? The script doesn’t seem to fire any errors and says its saving and loading correctly, except it isn’t.