I used this script to save player’s tools. but it didn’t work this is the script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")
game.Players.PlayerAdded:Connect(function(plr)
local tool = DataStore:GetAsync("User-"..plr.UserId)
if tool then
for i,v in pairs(tool) do
local ToolFound = game.ReplicatedStorage.BuyToolItems:FindFirstChild(v)
if ToolFound then
ToolFound:Clone().Parent = plr.Backpack
ToolFound:Clone().Parent = plr.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.Backpack)
end)
There also popped up an error
in the output: 104: Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters
how do I fix this?
That means you are trying to store an Instance (the backpack of the player), but you can only store values (strings, numbers)
So you could try to store a table containing the tools of a player:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")
game.Players.PlayerAdded:Connect(function(plr)
local success, data, err = pcall(function()
return DataStore:GetAsync("User-"..plr.UserId)
end)
if success then
for _, tool in pairs(data) do
local ToolFound = game.ReplicatedStorage.BuyToolItems:FindFirstChild(tool)
if ToolFound then
ToolFound:Clone().Parent = plr.Backpack
ToolFound:Clone().Parent = plr.StarterGear
end
end
else
error("Datastore error: "..err)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local plrBackpack = {}
for _, tool in pairs(Player.Backpack:GetChildren()) do
table.insert(plrBackpack, tool.Name)
end
local success, err = pcall(function()
DataStore:SetAsync(Player.UserId, plrBackpack)
end)
if not success then
error("Datastore error: "..err)
end
end)
I think it’s because the data is not set yet, so you could try this to check if data is a table:
if success then
if type(data) == "table" then
for _, tool in pairs(data) do
local ToolFound = game.ReplicatedStorage.BuyToolItems:FindFirstChild(tool)
if ToolFound then
ToolFound:Clone().Parent = plr.Backpack
ToolFound:Clone().Parent = plr.StarterGear
end
end
end
else
error("Datastore error: "..err)
end
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")
game.Players.PlayerAdded:Connect(function(plr)
local success, data, err = pcall(function()
return DataStore:GetAsync("User-"..plr.UserId)
end)
if success then
if type(data) == "table" then
for _, tool in pairs(data) do
local ToolFound = game.ReplicatedStorage.BuyToolItems:FindFirstChild(tool)
if ToolFound then
ToolFound:Clone().Parent = plr.Backpack
ToolFound:Clone().Parent = plr.StarterGear
end
end
end
else
error("Datastore error: "..err)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local plrBackpack = {}
for _, tool in pairs(Player.Backpack:GetChildren()) do
table.insert(plrBackpack, tool.Name)
end
local success, err = pcall(function()
DataStore:SetAsync(Player.UserId, plrBackpack)
end)
if not success then
error("Datastore error: "..err)
end
end)