Hi,
Hope you're all having a great new year so far! I'm trying to work on a game and I have a script that saves the tools in your backpack when you leave the game, but when I try to either add code to make the game save the player's variable data (Money and Knowledge), it doesn't work and breaks the entire script. I also tried putting it in a different script but it doesn't seem to work still. Could anyone help me out with this? This is the current script that saves all the tools
local DataStore = game:GetService("DataStoreService"):GetDataStore("Tools")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
game.ReplicatedStorage.PlayerReset:FireClient(player)
player:WaitForChild("InShop").Value = false
char.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
player.Equipped.Value = child.Name
end
end)
-- Give the tool back
if game.ServerStorage.Tools:FindFirstChild(player:WaitForChild("Equipped").Value) then
local toolToEquip = game.ServerStorage.Tools[player.Equipped.Value]:Clone()
toolToEquip.Parent = player:WaitForChild("Backpack")
end
end)
local inShop = Instance.new("BoolValue")
inShop.Name = "InShop"
inShop.Value = false
inShop.Parent = player
local playerToolsFolder = Instance.new("Folder")
playerToolsFolder.Name = player.Name
playerToolsFolder.Parent = game.ServerStorage.PlayerTools
local equipped = Instance.new("StringValue")
equipped.Name = "Equipped"
equipped.Parent = player
local tools = "tools-"..player.UserId
local equippedKey = "equipped-"..player.UserId
local data = nil
local success, errormessage = pcall(function()
data = DataStore:GetAsync(tools)
end)
if success then
print("Data for the tools was Found")
else
warn("Error"..errormessage)
end
local equippedData = nil
local success, errormessage = pcall(function()
equippedData = DataStore:GetAsync(equippedKey)
end)
if success then
print("Data for the tools was Found")
else
warn("Error"..errormessage)
end
if equippedData ~= nil then
equipped.Value = equippedData
end
if data ~= nil then
for i, tool in pairs(data) do
if game.ServerStorage.Tools:FindFirstChild(tool) then
local clonedTool = game.ServerStorage.Tools:FindFirstChild(tool):Clone()
clonedTool.Parent = game.ServerStorage.PlayerTools:FindFirstChild(player.Name)
end
end
else
print("New player")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tools = {}
for i, tool in pairs(game.ServerStorage.PlayerTools[player.Name]:GetChildren()) do
table.insert(tools,tool.Name)
end
local success, errormessage = pcall(function()
DataStore:SetAsync("tools-"..player.UserId,tools)
end)
if success then
print("data saved")
else
print("There was an error:"..errormessage)
end
local success, errormessage = pcall(function()
DataStore:SetAsync("equipped-"..player.UserId,player.Equipped.Value)
end)
if success then
print("data saved")
else
print("There was an error:"..errormessage)
end
print("Data successfully saved")
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down")
end
end
wait(10)
end)
Thanks!
