Help with this script



So, I have created a code in roblox studio that detects when an item is touched and saves the value in a folder, but the item is stored hundreds of times even though i want it to store only 1 time

Here is the script called “saving”

-- variables

local DataStoreService = game:GetService("DataStoreService")
local collectedDataStore = DataStoreService:GetDataStore("collected")
local replicatedStorage = game:GetService("ReplicatedStorage")
local itemlist = replicatedStorage:WaitForChild ("items")

game.Players.PlayerAdded:Connect(function(player)
	print("hi")
	local itemscollected = Instance.new("Folder", player)
	itemscollected.Name = "itemscollected"
	
	--Loading collected items
	local success, errormessage = pcall(function()
		local items = collectedDataStore:GetAsync("User-"..player.UserId)
		if items then
			for i, v in pairs(items) do
				local itemfound = itemlist: FindFirstChild(v)
				if itemfound then
					itemfound:Clone().Parent = itemscollected
				end
			end
		end
	end)
end)
game.Players.PlayerRemoving:Connect(function(player)
	--saving collected items
	local success, errormessage = pcall(function()
		local datasave = {}
		for i, item in pairs(player.itemscollected:GetChildren()) do
			if item then
				table.insert(datasave,item.Name)
			end
		end
		collectedDataStore:SetAsync("User-"..player.UserId, datasave)
	end)
end)


game:BindToClose(function(player)
	--saving collected items
	local success, errormessage = pcall (function()
		local datasave = {}
		for i, item in pairs(player.itemscollected:GetChildren()) do
			if item then
				table.insert(datasave, item.Name)
			end
		end
		collectedDataStore:SetAsync("User-"..player.UserId, datasave)
	end)
end)```





And here is the script called "collectscript"   
``` local item = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local itemlist = replicatedStorage:WaitForChild ("items")
local test1 = itemlist:WaitForChild("Test1")

item.Touched: Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local character = hit. Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			local datasave = player.itemscollected
			local found = datasave:FindFirstChild("test1")
			if not found then
				local item = test1:Clone()
				item.Parent = datasave
				item.Value = true
			end
		end
	end
end)```
1 Like

Change the parameter in the found’s :FindFirstChild()
The names are case-sensitive.

local found = datasave:FindFirstChild("Test1")

I know that many people find it annoying to explain some things, and since many people from the forum have left, they no longer provide help knowing about the subject, as @Korate168 said, he is calling the name wrong, and as a recommendation; call each instance with a different name

in which of the 2 scripts, thank you so much btw

Ahh alr thankyoouuu so muchhh :))

The script where you clone the object?

so the “local” names should be different from the ones i use in the other script?

What he means is that you write it incorrectly, you must take into account the capital letters.

No, the names you used to clone the instances,
FindFirstChild() finds the instance with the exact name.

folder:FindFirstChild("test1") -- results nil because there is no such thing as "test1" in the folder

could you maybe change the code to something that would work?
or could I maybe add you into the studio?

But how do I do that? the capitals of which words

In this script, line 12,
Change the line to exactly,

local found = datasave:FindFirstChild("Test1")
1 Like

in the part of local not found = dataSabe:FindFirstChild(“test1”) you pass it to local not found = dataSabe:FindFirstChild(“Test1”) --change the string

Thank you so much, but now it doesnt store anything at all

Thank you so much, but now it doesnt store anything at alll

Does the console print out anything?
If not, then try to print variables in the script to see what happened.

I think that error is in a different script now.

i have 2 items so that i know if that works