Why are the abilities and leaderstats folder keep replicating

I have 2 folders in the datastore, one called abilities, and one called leaderstats. However, they keep replicating. Does anyone know why?

my player added script

local userId = player.userId
	local key = "Player_" .. userId

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local abilities = Instance.new("Folder")
	abilities.Name = "Abilities"
	abilities.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "cash"
	local success, ret = 1, 1
		local success, ret = pcall(datastore.GetAsync, datastore, key)
	until success or not Players:FindFirstChild(player.Name)
	if success then
		cash.Value = ret or 0
		cash.Parent = leaderstats
		leaderstats.Parent = player
		print("There was an error! " .. ret)

What is this waitForRequestBudget Function? Also is this in a PlayerAdded function are is it wrapped around the event?

  1. Because the repeat is always trying it, the function is checking whether we have enough budget for that. 2. it’s a function, but i have a PlayerAdded:Connect(playerAdded)

Might ask when you say it replicating, do you mean the actual instances keep replicating?

Yup, the instances are duplicating itselves

And well, playing around with the scripts, they no longer do that, sorry for the late response

Well now the instances get duplicated again

Is the script in the ServerScriptService (Script) or is the script a local script?

The script is in serverscriptservice

I noticed that you parent the cash and leaderstat Instance after the loop is completed(otherwise when not Players:FindFirstChild(player.Name)) meaning it parents to a player instance thats been set to nil. Is this done purposefully?

well, that was done purposefully, but it doesnt make any changes to the duplication problem