Good morning, sir, I come to this forum to ask for help... I recently took a tutorial to get the basics of a game except I'm facing a problem. When I leave the game the tool data is saved but when I join the game the tool is not restored in the inventory as shown in the line : 23
I don't know why Thank you in advance.
local DataStore = game:GetService("DataStoreService"):GetDataStore("Tools")
local serverStorage = game:GetService("ServerStorage")
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.Players:FindFirstChild(player:WaitForChild("Equipped").Value) then
local toolToEquip = game.ServerStorage.Tools[player.Equipped.Value]:Clone()
toolToEquip.Parent = player:WaitForChild("Backpack")
print("Go")
end
print("No")
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = leaderstats
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 dataFolder = Instance.new("Folder")
dataFolder.Name = player.Name
dataFolder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = dataFolder
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local tools = "tools-"..player.UserId
local equippedKey = "equipped-"..player.UserId
local cashData, rebirthsData
local success,errormessage = pcall(function()
cashData = DataStore:GetAsync("money="..player.UserId)
rebirthsData = DataStore:GetAsync("rebirths="..player.UserId)
end)
if success then
if cashData then
cash.Value = cashData
rebirths.Value = rebirthsData
end
end
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 Tools 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 Equipped Saved")
else
print("There was an error:"..errormessage)
end
local success, errormessage = pcall(function()
DataStore:SetAsync("money="..player.UserId,player.leaderstats.Cash.Value)
DataStore:SetAsync("rebirths="..player.UserId,player.leaderstats.Rebirths.Value)
end)
if success then
print("data Money Saved")
else
print("There was an error:"..errormessage)
end
print("All 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)
