Hello everyone, I wrote this code to prevent losing items when a player respawns, resets, or dies. However, it doesn’t seem to work. Anyone got an idea?
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync("User-"..player.UserId)
if tool then
for i,v in pairs (tool) do
local toolFound = game.ServerStorage.Guns:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
end
end
end
end)
end)
if you just want a script that keeps tools in the inventory when a player dies, you can use this:
game.Players.PlayerAdded:Connect(function(player)
local backup = Instance.new(“Folder”,player)
backup.Name = “BackUp”
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
wait(8) --we wait a bit cuz it takes a while to actually respawn
local tools = player.BackUp:GetChildren()
for i = 1, #tools do
local tool = tools[i]
tool:Clone().Parent = player.Backpack
end
end)
end)
end)
Be sure to copy the tool into the backup folder when the player obtains the tool(s)
local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“BackpackSave”) – Change this with a different name.
game.Players.PlayerAdded:Connect(function(Player)
pcall(function()
local tool = DataStore:GetAsync(“User-”…Player.UserId)
if tool then
for i,v in pairs (tool) do
local toolFound = game.ServerStorage.Guns:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = Player.Backpack
toolFound:Clone().Parent = Player.StarterGear
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
pcall(function()
local tool = DataStore:GetAsync(“User-”…Player.UserId)
if tool then
for i,v in pairs (tool) do
local toolFound = game.ServerStorage.Guns:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = Player.Backpack
toolFound:Clone().Parent = Player.StarterGear
end
end
end
end)
})
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("BackpackSave") -- Change this with a different name.
game.Players.PlayerAdded:Connect(function(Player)
pcall(function()
local tool = DataStore:GetAsync("User-"..Player.UserId)
if tool then
for i,v in pairs (tool) do
local toolFound = game.ServerStorage.Guns:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = Player.Backpack
toolFound:Clone().Parent = Player.StarterGear
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
pcall(function()
local tool = DataStore:GetAsync("User-"..Player.UserId)
if tool then
for i,v in pairs (tool) do
local toolFound = game.ServerStorage.Guns:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = Player.Backpack
toolFound:Clone().Parent = Player.StarterGear
end
end
end
end)
})
end)
We do not need a datastore, that is wasting valuable space, since we do not want this data to stay after leaving the game. Instead go with this guys solution.
game.Players.PlayerAdded:Connect(function(player)
local backup = game.ServerStorage.Guns
backup.Parent = player
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
wait(8)
local tools = player.Guns:GetChildren()
for i = 1, #tools do
local tool = tools[i]
tool:Clone().Parent = player.Backpack
end
end)
end)
end)
yea as you can see in the script, i made the guns folder parent to be player as the backup. just change your script to the one i gave u and see if it works.