Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Backpack/Tool Saving Script | Argument 2 missing or nil?

16:55:00.942 - Argument 2 missing or nil 16:55:00.943 - Stack Begin 16:55:00.943 - Script 'ServerScriptService.toolSave', Line 103 16:55:00.943 - Stack End

This script is suppose to save the tools in the backpack for next time the player joins again

local rs = game:GetService("ServerStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local shopItems = rs:WaitForChild("ShopItems")
--< directory functions

local dir = {}

local function edit(player, list)
	dir[player.Name] = list
	
end

local function setup(player, list)
	for i = 1, #list do
		local tool = shopItems:FindFirstChild(list[i])
		if tool then
			local clone = tool:Clone()
			clone.Parent = player.Backpack
		else
			print(list[i] .. " not found")
		end
	end
end

game.Players.PlayerAdded:connect(function(player)
	local ready = false
	
		player.CharacterAdded:connect(function(char)
			
		local bp = player.Backpack
		local data = nil
		
		if ready == false then
			ready = true
			
			data = store:GetAsync(player.UserId)
			
			if data then
				setup(player, data)
				edit(player, data)
			end
		end
		
		char.Humanoid.Died:connect(function()
			char.Humanoid:UnequipTools()
			
			local old = player.StarterGear:GetChildren()
			for i = 1, #old do
				old[i]:Destroy()
			end
				
			local new = player.Backpack:GetChildren()
			for i = 1, #new do
				new[i]:Destroy()
			end
			
		end)
			
			local count = 0
			local function adjust()
				if char.Humanoid.Health > 0 then
				local list = {}
				
				local equipped = char.FindFirstChildOfClass("Tool")
				if equipped then
					table.insert(list, equipped.Name)
					
				end
				
				local tools = bp:GetChildren()
				for i = 1, #tools do
					table.insert(list, tools[i].Name)
				end
				
				if count ~= #list then
					edit(player, list)
					count = #list
				
				end
				end
				end
				
				bp.ChildAdded:connect(adjust)
				bp.ChildRemoved:connect(adjust)
				
				char.ChildAdded:connect(function(child)
					if child.ClassName == "Tool" then
						adjust()
				end
			end)
				
				char.ChildRemoved:connect(function(child)
					if child.ClassName == "Tool" then
						adjust()
			end
		end)
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
	store:SetAsync(player.UserId, dir[player.Name])
	dir[player.Name] = nil
end)

-- safety

game:BindToClose(function()
	wait(5)
end)

3 comments

0

What's with everyone following tutorials, Shiroos code is bad

0

I literally finished a Tool Persistence remake for someone, use mine: https://pastebin.com/TqM0Y6W9. Ensure you put a Folder within ServerStorage called Tools with all the tools you wish to have saved

0

I tried other tutorials to do the same thing but they also didn't work

2 answers

Well I'm kinda new at the Lua language but I believe that this is because you haven't declared what "player" means. I don't see where you declared

player = game.Players.LocalPlayer

Try putting that at the beginning of the script.

If that doesn't work, on line 104, you set

dir[player.Name] = nil

I'm not sure if that might be causing an issue because on the line previous, the second argument is dir[player.Name] That would mean that when the scripts runs and tries to grab that value, it gets nil which is causing the error.

Again, I'm kinda new at the Lua language but I hope this helps!

3 comments

0

Also, try putting it in the player's starter pack. I'm not sure how you've set the script but also try that if nothing works

0

Sir I commend you for trying, but the 'player' you're seeing is for a function's argument. Player is defined when he used PlayerAdded/PlayerRemoving. Keep up the good work though

0

Ohh I see, I skimmed right over that lol. Thank you for clarifying, I'm still learning????

Simply use the following to get all the tools in a table:

local tools = player.Backpack:GetChildren()
local char = player.Character

if char then
	local charParts = char:GetChildren()

	for key,value in pairs (charParts) do
		if value.isA("Tool") then
			table.insert(tools,1)
		end
	end
end

And then simply put this in a DataStore.

If this doesn't work, message me and I'll see if I can fix it.

1 comment

0

It don't see where the PlayerRemoving is?

This discussion is preserved as it was. New answers, comments, and votes are closed.