Why is my script not making leaderstats?

It worked fine a second ago any help?

local stat = "Pumpkins"
local startamount = 50
local stat2 = "Power"
local startamount2 = 1
local stat3 = "Coins"
local startamount3 = 1


local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")


game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Cash = Instance.new("IntValue",leader)
	Cash.Name = stat
	local Cash2 = Instance.new("IntValue",leader)
	Cash2.Name = stat2
	local Cash3 = Instance.new("IntValue",leader)
	Cash3.Name = stat3
	local PlayerData = ds:GetAsync(player.UserId)
	Cash2.Value = PlayerData and PlayerData.Power or startamount3
	Cash3.Value = PlayerData and PlayerData.Coins or startamount2
	Cash.Value = PlayerData and PlayerData.Pumpkins or startamount
	while true do
		wait(10)
		ds:SetAsync(player.UserId, {
			Pumpkins = Cash.Value,
			Power = Cash2.Value,
			Coins = Cash3.Value
		})
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, {
		Pumpkins = player.leaderstats.Pumpkins.Value,
		Power = player.leaderstats.Power.Value,
		Coins = player.leaderstats.Coins.Value
	})
end)

Is there any errors? Also, don’t save every 10 seconds. There’s a limit, do every 5 minutes (300 seconds) for auto save only.

the error is its not making folder in player

Been a while since I’ve touched Roblox scripts. From what I can tell, it’s the while loop doing it. The while loop never stops executing which causes the thread to hang on it exclusively, stopping the event from being able to listen for players being added.

Try this:

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Cash = Instance.new("IntValue",leader)
	Cash.Name = stat
	local Cash2 = Instance.new("IntValue",leader)
	Cash2.Name = stat2
	local Cash3 = Instance.new("IntValue",leader)
	Cash3.Name = stat3
	local PlayerData = ds:GetAsync(player.UserId)
	Cash2.Value = PlayerData and PlayerData.Power or startamount3
	Cash3.Value = PlayerData and PlayerData.Coins or startamount2
	Cash.Value = PlayerData and PlayerData.Pumpkins or startamount
end)

coroutine.wrap(function()
    while wait(10) do
        for _,player in (game.Players:GetPlayers()) do
            ds:SetAsync(player.UserId, {
                Pumpkins = player.leaderstats.Pumpkins.Value,
                Power = player.leaderstats.Power.Value,
                Coins = player.leaderstats.Coins.Value
            })
        end
    end
end)()

Other option
I’d also recommend, instead of saving every 10 seconds, to rather save when the player quits AND on Game#BindToClose just so the datastorages aren’t getting overwhelmed, which would also eliminate the need for a loop. You’ve already done half of this with the playerremoving area. If you were to do this, you’d have this instead.

local stat = "Pumpkins"
local startamount = 50
local stat2 = "Power"
local startamount2 = 1
local stat3 = "Coins"
local startamount3 = 1

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

function savePlayer(player) -- prevent code duplication
    ds:SetAsync(player.UserId, {
	Pumpkins = player.leaderstats.Pumpkins.Value,
	Power = player.leaderstats.Power.Value,
	Coins = player.leaderstats.Coins.Value
    })
end

game.Players.PlayerAdded:Connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Cash = Instance.new("IntValue",leader)
	Cash.Name = stat
	local Cash2 = Instance.new("IntValue",leader)
	Cash2.Name = stat2
	local Cash3 = Instance.new("IntValue",leader)
	Cash3.Name = stat3
	local PlayerData = ds:GetAsync(player.UserId)
	Cash2.Value = PlayerData and PlayerData.Power or startamount3
	Cash3.Value = PlayerData and PlayerData.Coins or startamount2
	Cash.Value = PlayerData and PlayerData.Pumpkins or startamount
end)

game.Players.PlayerRemoving:Connect(function(player)
	coroutine.wrap(savePlayer)(player) -- Do it on another stack so it doesn't interrupt other events
end)

game:BindToClose(function()
    if not game.RunService:IsStudio() and #game.Players:GetPlayers() > 1 then -- No need to execute if no players, and don't run in studio (remove that if you want it to execute in studio)
        for _, player in ipairs(game.Players:GetPlayers()) do
            coroutine.wrap(yourSaveFunction)(player)
        end
    end
end)

PlayerRemoving will execute when the player leaves or is kicked. BindToClose will execute then the server shuts down before the PlayerRemoving event can be fired.

Never really touched data store stuff, so if anyone has a better idea, better accept that one lol.