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)```