Keep Inventory Script (HELP)

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)
1 Like

What is inside that datastore?

You could follow this tutorial for saving the tools, How to Save Tools using Data Stores - Roblox Studio - YouTube

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)

1 Like

We don’t know if they want this to persist over leaving though.

Actually, try this

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)

1 Like

I do not, it’s simply a PVP map for groups. Groups can come and get free private servers for combat reasons. (So used for anything military related.)

I simply just want them to keep their items after dying, so they don’t have to keep going into the GUI.

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)

I do want a script that keeps the tools in the inventory, yes. However this does not seem to work.

I have this as a server script under Server Script Storage. Is this correct?

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.

1 Like

Yes, it has to be a server script, did you make sure that the tool(s) got cloned into the BackUp folder when the player gets the tool(s)?

you have to use a scirpt to put the tools in the backup folder or do it manually so it can actually save

1 Like

so you can change your script to this @JordxnSin

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)

I’m a bit confused here, I apologize.

The “Guns” folder is just a folder full of the tools (guns), so how would this also be a backup of a players inventory?

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.

Ok so, it works. However, it gives me every single gun. I only want it to give back the guns the player selected or already had in his inventory.

okay, do you have a value to detect if the player has the gun? if yes let me know the location

I do not, I am pretty new to scripting, if anything I am
basic so I have whatever you guys gave me lol.

oh wow, then try to make the script detect if the player backpack have changed so it gives the player the items that was in the backpack

1 Like

You may want to check out these basic tutorials on player inventory and persistent player inventory: