I dont need anything special, only that it saves your tools when you leave and loads them in when they join.
I also want to save on death specific tools.
How do I archive these goals?
Im really new in API services coding, so I dont know how to save them by myself sadly…
1 Like
Do you have tool templates somewhere?
1 Like
Because if yes here is the script with gears folder in ReplicatedStorage:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local database = DataStoreService:GetDataStore("YourDataStore")
local data = {}
local defaultData = {
["Tools"] = {}
}
local function LoadData(player)
player.CharacterAdded:Wait()
local success = nil
local playerData = nil
local attempt = 1
while not success and attempt <= 3 do
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
if not success then
attempt += 1
warn(playerData)
task.wait(1)
end
end
if success then
print("Data Loaded Successfully")
if not playerData then
print("New player, giving default data")
playerData = defaultData
end
data[player.UserId] = playerData
local backpack = player:WaitForChild("Backpack")
for _, toolName in pairs(data[player.UserId].Tools) do -- Load tools here
local toolTemplate = ReplicatedStorage.Gears:FindFirstChild(toolName)
if toolTemplate then
local NewTool = toolTemplate:Clone()
NewTool.Parent = backpack
else
warn("Tool template not found for:", toolName)
end
end
else
warn("Unable to get data for player", player.UserId)
player:Kick("There was a problem loading your data! Message the developer on Roblox if this problem continues!")
end
end
Players.PlayerAdded:Connect(LoadData)
local function saveData(player)
if data[player.UserId] then
print(data[player.UserId])
local success = nil
local attempt = 1
while not success and attempt <= 3 do
success = pcall(function()
database:SetAsync(player.UserId, data[player.UserId])
end)
if not success then
attempt += 1
warn("Attempt " .. attempt .. ": Unable to save data for player", player.UserId)
task.wait(1)
end
end
if success then
print("Data saved successfully for player", player.UserId)
else
warn("Failed to save data for player", player.UserId)
end
else
warn("No data found for player", player.UserId)
end
end
local function updatePlayerTools(player) -- update them
local tools = {}
local backpack = player:FindFirstChild("Backpack")
if backpack then
for _, item in pairs(backpack:GetChildren()) do
if item:IsA("Tool") then
table.insert(tools, item.Name)
end
end
data[player.UserId].Tools = tools
end
end
Players.PlayerRemoving:Connect(function(player)
updatePlayerTools(player)
saveData(player)
data[player.UserId] = nil
end)
game:BindToClose(function() -- server shutdown
if not RunService:IsStudio() then
for _, player in pairs(Players:GetPlayers()) do
task.spawn(function()
updatePlayerTools(player)
saveData(player)
end)
end
else
print("Roblox Studio")
end
end)
3 Likes
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.