#Tools not saving when the player holds them
1 messages · Page 1 of 1 (latest)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToolStore = DataStoreService:GetDataStore("PlayerTools_V3")
local ToolFolder = ReplicatedStorage:WaitForChild("Tools")
local PlayerToolMemory = {}
local function registerTool(player, tool)
if not tool:IsA("Tool") then return end
PlayerToolMemory[player] = PlayerToolMemory[player] or {}
PlayerToolMemory[player][tool.Name] = true
end
local function watchContainer(player, container)
container.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
registerTool(player, child)
end
end)
end
local function onPlayerAdded(player)
PlayerToolMemory[player] = {}
player:WaitForChild("Backpack")
watchContainer(player, player.Backpack)
player.CharacterAdded:Connect(function(character)
watchContainer(player, character)
end)
local success, saved = pcall(function()
return ToolStore:GetAsync(player.UserId)
end)
if success and type(saved) == "table" then
for toolName in pairs(saved) do
local template = ToolFolder:FindFirstChild(toolName)
if template then
template:Clone().Parent = player.Backpack
end
end
end
end
local function onPlayerRemoving(player)
local owned = {}
local backpack = player:FindFirstChild("Backpack")
if backpack then
for _, tool in ipairs(backpack:GetChildren()) do
if tool:IsA("Tool") then
owned[tool.Name] = true
end
end
end
local character = player.Character
if character then
for _, tool in ipairs(character:GetChildren()) do
if tool:IsA("Tool") then
owned[tool.Name] = true
end
end
end
local success, err = pcall(function()
ToolStore:UpdateAsync(player.UserId, function()
return owned
end)
end)
if not success then
warn("Failed to save tools:", err)
end
end
game:BindToClose(function()
for _, player in ipairs(Players:GetPlayers()) do
onPlayerRemoving(player)
end
end)
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
Perhaps char isn’t loaded yet
bullshit
where is datastoreservice in the workspace?
woah chill bro
In the script he does update a sync and get a sync
@neon portal
I am pretty sure the reason why your script isn't working is due 2 the onPlayerRemoving function you should do SetAsync not UpdateAsync
oh?
nope
gotta love roblox studio 🙏