:Clone() cloning Value instance more than once

I want to clone two values in replicated storage and make them a child of the player.

The issue is that it clones them multiple times, so it messes with the script.

I tried using debounce and it didn’t work for me and some solutions I looked for didn’t help either.

local function detectPlayerValueChanged(plr, plr2, KC1, KC2)
	local cloned = KC1:Clone()
	local cloned2 = KC2:Clone()
	cloned.Parent = plr
	cloned2.Parent = plr2

	plr.Store.KO.Changed:Connect(function(newVal)
		print("Plr1 Value Changed")
		cloned.Value += 1
	end)

	plr2.Store.KO.Changed:Connect(function(newVal)
		print("Plr2 Value Changed")
		cloned2.Value += 1
	end)
end
1 Like

Can I see the full script? Maybe your feeding in the same player in the first to arguements of your function.

1 Like

I put the two players in the list table as the first two arguments.

The script works it’s just that it clones the value instances more than 4 times.

local MatchInit = {}

--Server Storage
local Rep = game:GetService('ReplicatedStorage')
local Players = game:GetService('Players')
--Folders
local Values = Rep:WaitForChild('Values')
local Mods = Rep:WaitForChild('Modules')
local Events = Rep:WaitForChild('Events')
local Spawns = workspace:WaitForChild('SpawnLocations')
--Variables
local L = Values:WaitForChild('Loser')
local R = Values:WaitForChild('Round')
local W = Values:WaitForChild('Winner')
local C = Values:WaitForChild('CountDown')
local F = Values:WaitForChild('FirstCount')
local S = Values:WaitForChild('SecondCount')
local Base = Spawns:WaitForChild('BaseSpawn')
local A1Folder = Spawns:WaitForChild('Area1Spawns')
local A2Folder = Spawns:WaitForChild('Area2Spawns')
local MS = Events:WaitForChild('MatchStart')
local ME = Events:WaitForChild('MatchEnd')
local GM = require(Mods:WaitForChild('GameMethods'))
--Code
List = {}

local function detectPlayerValueChanged(plr, plr2, KC1, KC2)
	local cloned = KC1:Clone()
	local cloned2 = KC2:Clone()
	cloned.Parent = plr
	cloned2.Parent = plr2

	plr.Store.KO.Changed:Connect(function(newVal)
		print("Plr1 Value Changed")
		cloned.Value += 1
	end)

	plr2.Store.KO.Changed:Connect(function(newVal)
		print("Plr2 Value Changed")
		cloned2.Value += 1
	end)
end

local function checkPlayers()
	if #List == 1 then
		print("A player won")
		ME:Fire(GM.FinishMethods.Finished)
	end
end

local function checkPlayerLeft()
	for i, v in ipairs(List) do
		Players.PlayerRemoving:connect(function(plr)
			if v == plr then
				ME:Fire(GM.FinishMethods.PlayerLeft)
				print(plr.." ".."Left")
			else
				
			end
		end)
	end
end

local function removeList(player)
	for i,v in ipairs(List) do
		if v == player then
			table.remove(List,i)
			checkPlayerLeft()
			checkPlayers()
		end
	end
end

local function moveToLobby(player)
	player.RespawnLocation = Base
	player:LoadCharacter()
end

local function startSpawn(player)
	player.RespawnLocation = Base
end

local function addPlayer(player,spawnFolder)
	local debounce = false
	player.RespawnLocation = spawnFolder
	player:LoadCharacter()
	
	local char = player.Character
	
	detectPlayerValueChanged(List[1],List[2],F,S)	
end

function MatchInit.SendPlayers(folder,plrToAdd)
	local AS = folder:GetChildren()
	
	table.insert(List, plrToAdd)
	if table.find(List,List[1],2) then
		table.remove(List,2)
	else
		
	end
	
	repeat 
		wait()
	until #List == GM.Minimum
	print(#List)
	wait(GM.TransitionTime)
	
	local spawn1 = AS[1]
	local spawn2 = AS[2]
	
	for i, v in ipairs(List) do
		if v == List[1] then
			addPlayer(v, spawn1)
			else
		end
		if v == List[2] then
			addPlayer(v, spawn2)
			else
		end
	end
end

Players.PlayerAdded:Connect(startSpawn)
return MatchInit

Do the values have the same name?

Name the clone1 and clone2 to different things then look in your player to see if the clone is the same or different.

I looked in the players already and the values are different. KC1 is given to the first player in the list and the value I assigned to it is called “FirstCount” and KC2 is given to the second player as the value called “SecondCount”.

So when you mean its cloning many times, do you mean that your getting multiple
first counts and the other player is getting multiple second counts?

Yes, so when the match starts they should only get 1 copy of those values but instead they get 4 or more copies.