Trying to fix cloning issue [NEW ISSUE Look down at bottom post for the issue]

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

  1. I just want my script to clone once, that’s it. Keep it simple and clear!

  2. My script is cloning a ton and I only want it to clone the tool once.? image Screenshot by Lightshot Include screenshots / videos if possible!

  3. Tried removing some clones didn’t work, also it seems to be accessing the datastore way too many times. Did you look for solutions on the Developer Hub? Yes.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Heres the script that I wrote, I need some help figuring out why these bugs are happening.

local playersindatastore = {}
local founduserids = {}
game.Players.PlayerAdded:Connect(function(plr)
	local DataStoreService = game:GetService("DataStoreService")
local playersentered = DataStoreService:GetOrderedDataStore("PlayersEnteredDataStore")
local success, errormessage = pcall(function()
		local pages =  playersentered:GetSortedAsync(false,100)
		local hundred = pages:GetCurrentPage()
		 function saveplayer(data)
				playersentered:SetAsync("playernumber"..plr.UserId,data)
		end 
		for rank,data in ipairs(hundred) do
			table.insert(playersindatastore,rank,plr.UserId)
		end
		for i,userid in pairs(playersindatastore) do
			local found = false
			if userid == plr.UserId then
				found = true
				table.insert(founduserids,i,userid)
			else if userid ~= plr.UserId and i == 100 then
					--Missed out on the 100 player thing
				else if userid ~= plr.UserId and #playersindatastore ~= 100 then
						saveplayer(i)
					end
				end
			end
		end
		for i,v in pairs(founduserids) do
 			if v == plr.UserId then
				plr.CharacterAdded:Connect(function(char)
					local tool = game.ServerStorage["100PlayerTool"]:Clone()
					tool.Parent = plr.Backpack
					print("cloned")
				end)
			end
		end
	end)
	if success then
		print("did did")
		if #playersindatastore == 0 then
			saveplayer(1)
			local tool = game.ServerStorage["100PlayerTool"]:Clone()
				tool.Parent = plr.Backpack
				print("cloned")
	else if not success then
			error(errormessage)
		end
	end 
	end
	end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

It seems that you are not resetting your playersindatastore variable when a new player joins. Unless I am mistaken, whenever a player is added you just table.insert() the entire list again on top of itself. This causes you to have multiple of the same UserId in your playersindatastore table. This could cause a player to have multiple items, depending on how many people join the server.

Ya thats pretty much what happens, fixing that now.

1 Like

image
Didn’t work

Are you testing this in roblox studio?

Yes, I am testing in roblox studio.

Where is that “Top100Players:33” coming from? Have you already tried printing all of the UserIds from the datastore to make sure yours is not in there multiple times?

image
image uhoh

Wait a second. Are you inserting your players UserId into playersindatastore for every key?

Screenshot_2020-07-08-22-17-40
Shouldn’t you be adding the UserId of the player from the datastore, not your own player’s? You are using the “plr.UserId” from the PlayerAdded event.

Players in datastore is infact the datastore grabbing but it loops through that and in the found userids table its only the user ids wait WHY DO I NEED THIS OTHER TABLE

But every UserId inside playersindatastore is going to equal your own.

No. That’s not true, its grabbing from the real datastore, not from the players in the game. WAIT IM A STUPID

Screenshot_2020-07-08-22-28-15

Thank you so much, you fixed it :smiley:

Yay! I’m so happy we were able to figure it out. :grinning:

Me too, thank you so much for your help gn

Ok now, instead of cloning a bunch of times, its not cloning at all, the old script clones a million times, this one does nothing, it’s probably a problem with the datastore.

game.Players.PlayerAdded:Connect(function(plr)
	local playersindatastore = {}
local founduserids = {}
	local DataStoreService = game:GetService("DataStoreService")
local playersentered = DataStoreService:GetOrderedDataStore("PlayersEnteredDataStore")
local success, errormessage = pcall(function()
		local pages =  playersentered:GetSortedAsync(false,100)
		local hundred = pages:GetCurrentPage()
		 function saveplayer(data)
				playersentered:SetAsync("playernumber".."|"..plr.UserId,data)
		end 
		for rank,data in ipairs(hundred) do
			table.insert(playersindatastore,rank,string.split(data.key,"|")[2])
		end
		for i,userid in pairs(playersindatastore) do
			local found = false
			if userid == plr.UserId then
				found = true
				table.insert(founduserids,i,userid)
			else if userid ~= plr.UserId and i == 100 then
					--Missed out on the 100 player thing
				else if userid ~= plr.UserId and #playersindatastore ~= 100 then
						saveplayer(i)
					end
				end
			end
		end
		for i,v in pairs(founduserids) do
			print(v)
 			if v == plr.UserId then
				plr.CharacterAdded:Connect(function(char)
					local tool = game.ServerStorage["100PlayerTool"]:Clone()
					tool.Parent = plr.Backpack
					print("cloned")
				end)
			end
		end
	end)
	if success then
		print("did did")
		if #playersindatastore == 0 then
			saveplayer(1)
			local tool = game.ServerStorage["100PlayerTool"]:Clone()
				tool.Parent = plr.Backpack
				print("cloned")
	else if not success then
			error(errormessage)
		end
	end 
	end
	end)

Any errors? You might have to convert the id from a string into a number with “tonumber()”.

for rank,data in ipairs(hundred) do
		table.insert(playersindatastore,rank,tonumber(string.split(data.key,"|")[2]))
end
1 Like

Edited the code to add to number, it gets a little bit further in the script but then plr.CharacterAdded:Connect code never gets called.

local playersindatastore = {}
local founduserids = {}
game.Players.PlayerAdded:Connect(function(plr)
	local DataStoreService = game:GetService("DataStoreService")
local playersentered = DataStoreService:GetOrderedDataStore("PlayersEnteredDataStore")
local success, errormessage = pcall(function()
		local pages =  playersentered:GetSortedAsync(false,100)
		local hundred = pages:GetCurrentPage()
		 function saveplayer(data)
				playersentered:SetAsync("playernumber".."|"..plr.UserId,data)
		end 
		for rank,data in ipairs(hundred) do
			table.insert(playersindatastore,rank,tonumber(string.split(data.key,"|")[2]))
		end
		for i,userid in pairs(playersindatastore) do
			print(userid)
			local found = false
			if tonumber(userid) == plr.UserId then
				found = true
				table.insert(founduserids,i,userid)
			else if userid ~= plr.UserId and i == 100 then
					--Missed out on the 100 player thing
				else if userid ~= plr.UserId and i == #playersindatastore and #playersindatastore ~= 100 then
						saveplayer(i)
					end
				end
			end
		end
		for i,v in pairs(founduserids) do
 			if tonumber(v) == plr.UserId then
				plr.CharacterAdded:Connect(function(char)
					local tool = game.ServerStorage["100PlayerTool"]:Clone()
					tool.Parent = plr.Backpack
					print("cloned")
				end)
			end
		end
	end)
	if success then
		print("did did")
		if #playersindatastore == 0 then
			saveplayer(1)
			local tool = game.ServerStorage["100PlayerTool"]:Clone()
				tool.Parent = plr.Backpack
				print("cloned")
	else if not success then
			error(errormessage)
		end
	end 
	end
	end)

So, theres a problem with the plr.CharacterAdded Code because I tried it with debugging.
Heres where it got.image