Datastore with CollectionService?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I have a CollectionService with a bunch of Collectible parts with the same name, and when you touch the part it makes it Invisible and you cannot touch it, I want to DataStore this so Collectibles that the player have already Collected cannot be Collected again, and those that hasn’t been Collected can still be Collected.

  1. What is the issue? Include screenshots / videos if possible!

When I collect one collectible and save and load back in, I can’t collect the collectibles I haven’t collected yet.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have looked for solutions everywhere and have found nothing.

For clarification, This DataStore has different Save Slots
This is my script:

for i, Collectible in pairs(CS:GetTagged("Collectible")) do
	
	---------GetAsync
	event.OnServerEvent:Connect(function(plr, typeOf)
		if typeOf == "slot4" then
			local success, errormessage = pcall(function()
				print("worked")
				local data = DataStore:GetAsync(plr.UserId .. "-slot1")
				if data ~=  nil then
					Coins.Value = data[1]  ------Ignore
					Collectibles.Value = data[2] -----Ignore
					Collectible.Transparency = data[3] 
					Collectible.CanTouch = data[4]
				else
					Coins.Value = 0
					Collectibles.Value = 0
				end
			end)
			if success then
				print("success")
			end
			if errormessage then
				warn(errormessage)
			end
		end
	end)

	event.OnServerEvent:Connect(function(plr, typeOf)
		if typeOf == "slot5" then
			local success, errormessage = pcall(function()
				print("worked")
				local data = DataStore:GetAsync(plr.UserId .. "-slot2")
				if data ~= nil then
					Coins.Value = data[1]
					Collectibles.Value = data[2]
					Collectible.Transparency = data[3]
					Collectible.CanTouch = data[4]
				else
					Coins.Value = 0
					Collectibles.Value = 0
				end
			end)
			if success then
				print("success")
			end
			if errormessage then
				warn(errormessage)
			end
		end
	end)

	event.OnServerEvent:Connect(function(plr, typeOf)
		if typeOf == "slot6" then
			local success,errormessage = pcall(function()
				print("worked")
				local data = DataStore:GetAsync(plr.UserId .. "-slot3")
				if data ~= nil then
					Coins.Value = data[1]
					Collectibles.Value = data[2]
					Collectible.Transparency = data[3]
					Collectible.CanTouch = data[4]
				else
					Coins.Value = 0
					Collectibles.Value = 0
				end
			end)
			if success then
				print("Success")
			end
			if errormessage then
				warn(errormessage)
			end
		end
	end)
	
	---------SetAsync
	Sevent.OnServerEvent:Connect(function(plr)
		print("worked")
		DataStore:SetAsync(plr.UserId .. "-slot1", {Coins.Value, Collectibles.Value, Collectible.Transparency, Collectible.CanTouch})
	end)

	Sevent2.OnServerEvent:Connect(function(plr)
		print("worked")
		DataStore:SetAsync(plr.UserId .. "-slot2", {Coins.Value, Collectibles.Value, Collectible.Transparency, Collectible.CanTouch})
	end)

	Sevent3.OnServerEvent:Connect(function(plr)
		print("worked")
		DataStore:SetAsync(plr.UserId .. "-slot3", {Coins.Value, Collectibles.Value, Collectible.Transparency, Collectible.CanTouch})
	end)
	
	game:BindToClose(function()
		for i, plr in pairs(game.Players:GetPlayers()) do

			DataStore:SetAsync(plr.UserId .. "-slot1", {Coins.Value, Collectibles.Value, Collectible.Transparency, Collectible.CanTouch})
		end
	end)
end

Any help is appreciated

1 Like

add id to all parts via loop, they always be the same in loop if they are the same name, then make table like

local parts = {}
--loop
parts[i] = v 
-- end

then save this table to data store and after rejoining, go through the parts again and check if it’s in table, if yes, delete it on client or something

1 Like

So, like this?

local CollectibleParts = {}

for i, Collectible in pairs(CS:GetTagged("Collectible")) do
	CollectibleParts[i] = Collectible
end

event.OnServerEvent:Connect(function(plr, typeOf)
	if typeOf == "slot4" then
		local success, errormessage = pcall(function()
			print("worked")
			print(CollectibleParts)
			local data = DataStore:GetAsync(plr.UserId .. "-slot1")
			if data ~=  nil then
				Coins.Value = data[1]  ------Ignore
				Collectibles.Value = data[2] -----Ignore
				CollectibleParts = data[3]
			else
				Coins.Value = 0
				Collectibles.Value = 0
			end
		end)
		if success then
			print("success")
		end
		if errormessage then
			warn(errormessage)
		end
	end
end)

event.OnServerEvent:Connect(function(plr, typeOf)
	if typeOf == "slot5" then
		local success, errormessage = pcall(function()
			print("worked")
			print(CollectibleParts)
			local data = DataStore:GetAsync(plr.UserId .. "-slot2")
			if data ~= nil then
				Coins.Value = data[1]
				Collectibles.Value = data[2]
				CollectibleParts = data[3]
			else
				Coins.Value = 0
				Collectibles.Value = 0
			end
		end)
		if success then
			print("success")
		end
		if errormessage then
			warn(errormessage)
		end
	end
end)

event.OnServerEvent:Connect(function(plr, typeOf)
	if typeOf == "slot6" then
		local success,errormessage = pcall(function()
			print("worked")
			print(CollectibleParts)
			local data = DataStore:GetAsync(plr.UserId .. "-slot3")
			if data ~= nil then
				Coins.Value = data[1]
				Collectibles.Value = data[2]
				CollectibleParts = data[3]
			else
				Coins.Value = 0
				Collectibles.Value = 0
			end
		end)
		if success then
			print("Success")
		end
		if errormessage then
			warn(errormessage)
		end
	end
end)


Sevent.OnServerEvent:Connect(function(plr)
	print("worked")
	DataStore:SetAsync(plr.UserId .. "-slot1", {Coins.Value, Collectibles.Value, CollectibleParts})
end)

Sevent2.OnServerEvent:Connect(function(plr)
	print("worked")
	DataStore:SetAsync(plr.UserId .. "-slot2", {Coins.Value, Collectibles.Value,CollectibleParts})
end)

Sevent3.OnServerEvent:Connect(function(plr)
	print("worked")
	DataStore:SetAsync(plr.UserId .. "-slot3", {Coins.Value, Collectibles.Value, CollectibleParts})
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do

		DataStore:SetAsync(plr.UserId .. "-slot1", {Coins.Value, Collectibles.Value, CollectibleParts})
	end
end)
1 Like

Pretty sure he means to have each collectible have a UID (Unique Identifier). Which could be even the collectibles names as long as the names dont repeat, basiaclly you should save the collectibles in a table like

CollectibleTable = {}
table.insert(CollectibleTable, Collectible.Name)

You would then save the collectibletable when the player leaves or however you do the saving and when they get in game load the table back up and then you can loop through the table or when they are about to get a collectible check if its already in the table

if not table.find(CollectibleTable, Collectible.Name) then
-- Collects item and inserts it into table
else
-- Doesn't collect item because collectible was already found in table
end
1 Like

yea, you should know that if collectibles are placed by you, and you don’t change their parent, then always for loop will go through all of them the same, soo table[i] always is specific v, in this case your collectible

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.