Need Help With My code

So im trying to put in this tool saver but it wants to have the tools in a folder
But that conflicts with my Buying script.

local ds = game:GetService("DataStoreService")
local coinDs = ds:GetDataStore("Coins")
local cashDs = ds:GetDataStore("Cash")


local lighting = game:GetService("ServerStorage")

game.Players.PlayerAdded:Connect(function(plr)
local id = game.Players:GetUserIdFromNameAsync(plr.Name)
local player = game.Players:FindFirstChild(plr.Name)	
local coinsKey = "Coins_"..id
local coins = coinDs:GetAsync(coinsKey)
if coins ~= nil then

else
	game.ServerStorage.Coin:Clone().Parent = player.Backpack
end	
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	
	
end)```
And here is the other scripts 
This script gives the tool 
```local player = game.Players.LocalPlayer

script.Parent.Close.MouseButton1Click:Connect(function()
	local camera = game.Workspace.CurrentCamera	
	camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	camera.CameraType = "Custom"
	script.Parent.Parent.Enabled = false
	
end)

script.Parent["Quick Button"].MouseButton1Click:connect(function()
	if game.Players:FindFirstChild(player.Name).leaderstats.Cash.Value >=100 
		and game.Players:FindFirstChild(player.Name).Backpack:FindFirstChild("Coin Quick")==nil 
		and game.Workspace:FindFirstChild(player.Name):FindFirstChild("Coin Quick")==nil then
		game.ReplicatedStorage.Purchase.Purchase1:FireServer()
		game.ServerStorage.Tools:FindFirstChild("Coin Quick"):Clone().Parent = game.Players:FindFirstChild(player.Name).Backpack
	end
end)``` 
And heres the open source tool script i got 
```--[[
5/2/2020
β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ•—β–‘β–‘β–‘β–‘β–‘  β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ•—β–‘β–‘β–‘β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘
β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–‘β–‘  β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—
β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–‘β–‘  β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•—β–‘β–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•
β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β–‘β–‘  β–‘β•šβ•β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–‘β–ˆβ–ˆβ•”β•β•β•β–‘β–‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—
β–‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β–‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘β–‘β–‘β•šβ–ˆβ–ˆβ•”β•β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–‘β–‘β–ˆβ–ˆβ•‘
β–‘β–‘β–‘β•šβ•β•β–‘β–‘β–‘β–‘β•šβ•β•β•β•β•β–‘β–‘β•šβ•β•β•β•β•β–‘β•šβ•β•β•β•β•β•β•  β•šβ•β•β•β•β•β•β–‘β•šβ•β•β–‘β–‘β•šβ•β•β–‘β–‘β–‘β•šβ•β•β–‘β–‘β–‘β•šβ•β•β•β•β•β•β•β•šβ•β•β–‘β–‘β•šβ•β• Version 1.0
						by guywithapoo
					
Thank you for using Tool Saver! I hope this small kit will help beginning developers create big things in the future.
If you find any issues, please message guywithapoo on Roblox.
A few notes:
- This kit does not save the tools when a player leaves, only when they die. If this is a feature you would like to be added, 
please message guywithapoo!
- If someone has more than 1 copy of a tool, it will only save one.
- The function "loadTools" will find all of the tools in a few specific areas where stuff could be stored. Areas Include: 
Workspace, ReplicatedStorage, and ServerStorage. If you would like to add more areas to search for tools, take the example in 
the function and edit it accordingly.
- I will be providing commas in certain areas to help explain what I am doing.

Also, if you would like to exclude certain tools from being saved, go to line 108 to see where you can exclude them.

--]]





print("Thanks for using ToolSaver by guywithapoo!")
if script.Parent ~= game.ServerScriptService then -- if it isnt already in ServerScriptService it will put itself there.
	script.Parent = game.ServerScriptService
end

if not game.ServerStorage:FindFirstChild("Tools") then -- if there isnt a tools folder in ServerStorage its gonna make one.
	Tools = Instance.new("Folder")
	Tools.Parent = game.ServerStorage
	Tools.Name = "Tools"
end

function loadTools() 
	--[[ --<< REMOVE THESE IF YOU ADD MORE!
	for i,v in pairs (EXAMPLE:GetDescendants()) do 
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	--]] --<< REMOVE THESE IF YOU ADD MORE!
	for i,v in pairs (workspace:GetDescendants()) do -- searches for Tools in the workspace
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	for i,v in pairs (game.ReplicatedStorage:GetDescendants()) do -- searches for Tools in ReplicatedStorage
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	for i,v in pairs (game.ServerStorage:GetDescendants()) do -- searches for Tools in ServerStorage
		if v.ClassName == "Tool" and v.Parent ~= game.ServerStorage.Tools then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end
	for i,v in pairs (game.Lighting:GetDescendants()) do -- searches for Tools in Lighting
		if v.ClassName == "Tool" then
			local clone = v:Clone()
			clone.Parent = game.ServerStorage.Tools
		end
	end

	

end
loadTools() -- fires the loadtools function






game.Players.PlayerAdded:Connect(function(player) -- this can be left alone, unless you are trying to experiment and learn :)
	local tools_table = { -- a dictionary, lookup on the wiki
	}
	player.CharacterAdded:Connect(function(character)
		for i,v in pairs (tools_table) do 
			-- when a player is added, it is checking the dictionary and loading in the items after death.
			-- print(i,v)
			if game.ServerStorage.Tools:FindFirstChild(i) then
				local clone = game.ServerStorage.Tools[i]:Clone()
				clone.Parent = player.Backpack
			end
		end
		character:FindFirstChild("Humanoid").Died:Connect(function() 
			-- print(player,"died.")
			-- when a player dies, it is going to add the names of the items they have in Backpack to the dictionary.
			for _,tool in pairs (player.Backpack:GetChildren()) do
				if not tools_table[tool.Name] then
					tools_table[tool.Name] = true
				end
				
			end
			
			for i,v in pairs (tools_table) do -- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< EXCLUDE CERTAIN ITEMS!
				-- this excludes certain items.
				if i == "EXAMPLE" or i == "EXAMPLE2" or i == "EXAMPLE3" then -- change EXAMPLE, and add more if you need to exclude more
					--print("removed:",i)
					tools_table[i] = nil
				end
 			end

			for i,v in pairs (tools_table) do 
				-- this removes an item from the dictionary if they dropped it to prevent duplication.
				if not player.Backpack:FindFirstChild(i) then
					--print("removed:",i)
					tools_table[i] = nil
				end
 			end

			for _,tool in pairs (character:GetChildren()) do
				-- checks inside of the character and saves that too, just in case if they are holding an item when they die.
				if tool.ClassName == "Tool" then
					if not tools_table[tool.Name] then
						tools_table[tool.Name] = true
					end
			
				end
			end
		end)
	end)
end)


1 Like
--identify datastore
local dataservice = game:GetService("DataStoreService")
local tooldata = dataservice:GetDataStore("ToolsData")

-- create folder
local toolsfolder = game.ReplicatedStorage.DefaultTools

game.Players.PlayerAdded:Connect(function(plr)
	-- gather plrdata
	local savedtools = tooldata:GetAsync(plr.UserId .. "-t") or {}
	--loop through saved tools and add to backpack and startergear
	for i, savedtools in pairs(savedtools) do
		if toolsfolder:FindFirstChild(savedtools) then
			toolsfolder[savedtools]:Clone().Parent = plr.Backpack
			toolsfolder[savedtools]:Clone().Parent = plr.StarterGear 
		end
	end
	--unequip tools so they save even equipped tools
	plr.CharacterRemoving:Connect(function(char)
		char.Humanoid:UnequipTools()
	end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
	--identify table of tools to save
	local toolstosave = {}
	--loop through backpack to get tools, and then add names to table
	for i, v in pairs(plr.Backpack:GetChildren()) do
		table.insert(toolstosave, v.Name)
	end
	-- save data
	local s, f = pcall(function()
		tooldata:SetAsync(plr.UserId .. "-t", toolstosave)
	end)
end)

Be sure to make a FOLDER in REPLICATEDSTORAGE with all of the tools that could be saved and reloaded in. Do this or it won’t work.

No idea why that tool adder is so out of the way, just steer clear of those types of scripts.

2 Likes

I have them in a folder its just that it conflicts with the two other scripts

What are the other values you would like to save and where are you storing them once a user starts a session?

It saves your tool when you die i wanna try further testing if i can get it to work

You dont need dss when you saving tool when u die. u can just use table

I am a Novice At coding so I don’t think i know how to use a table

Here are some several mistakes you made.

First, data stores can store multiple data, so you don’t need to separate the datas into one by one.

local dataStore = ds:GetDataStore(β€œDataStore”)

Then, you should probably insert the folder inside that event. Not just seperate.

You should use a pcall function to prevent loading data errors. Also, you should probably use an empty variable, so you could refer to load multiple datas.

local data
local success, error = pcall(function()
    data = ds:GetAsync(id)
end)

if success then
    β€” load your data here
else
    warn(β€œfailed to load data.”)
end

Is the coin an IntValue, or a tool? I’m confused.

You could try to move this code in the PlayerAdded event.

    local ToolsFolder = Instance.new(β€œFolder”)
    ToolsFolder.Name = β€œTools”
    ToolsFolder.Parent = id

    β€” in here, you could insert a BoolValue to check if the player has a tool already and want to load if the value is a true.

    local haveTool = Instance.new(β€œBoolValue”)
    haveTool.Name = β€œhaveTool”
    haveTool.Name = ToolsFolder

Now when trying to load the data…

if success then
    β€” load your IntValues data and haveTools value  here
    if not haveTools then
        β€” Clone The tool in here and parent it to player, in this case parent to id
        haveTool.Value = true
    end
end

Now when trying to save the data… (include this in PlayerRemoving event)

local ID = plr.UserId
local data = {
    cash = ID.leaderstats.Cash.Value
    β€” other data’s here
    haveTools = ID.Tools.haveTools.Value
}

local success, error = pcall(function()
    ds:SetAsync(ID,data)
end)

if success then
    print(β€œsaved data successfully.”)
else
    warn(β€œthere was an error on saving data.”)

You should need to know about tables if your using DataStoreService service to do stuffs.

Hope these helps.

1 Like

The β€œCoins” Are a tool For my game not a integer

Then you could try to use the coins as a β€œtool” and change some settings of my script.

1 Like

Is this all one script or multiple if so local or just script?

All in one server script. You should try to understand more about DataStoreService from the tutorial. It seems like your only starting to script and needs to know a lot of more stuffs.

Yeah I’m learning how to script as you can see my low knowledge Its kind of hard for me sometimes to understand I seen to get the hang of it if its hands on so ill give it a try.

1 Like

I see. Good luck on it! Also did my script worked?

I have to open it and give it a try and also edit it so it lines up with the coin tools

1 Like

Im now confused all the coins i have different names for each What would i do?

Remove this, Ive already provided the fix and instance for this in my post.

Walk me through how to do it. I dont understand

How many tools do you have in your game? Is it only just that Coins tool?

I have seven Coins and possible more.