I have a problem where one of my datastore scripts go on a loop. It repeatedly saves.

I have print() to show up when it saves, and it shows in the output. But it goes over and over infinitely. None of my other datastore scripts do this, and they're the same scripts just saving different values. Here's the one that's looping:

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("leveldatasaveforedgame")
game.Players.PlayerAdded:connect(function(player)





local Level  = Instance.new("IntValue",player)
Level.Name = "Level"
Level.Value = ds1:GetAsync(player.UserId) or 0
ds1:SetAsync(player.UserId, Level  .Value)
Level.Changed:connect(function()
	print("Level  Process Begin")

	ds1:SetAsync(player.UserId, Level.Value)
	print(player.Name .. "   has had   " .. Level.Value .. "  saved as Level  User ID is  " .. player.UserId)


end)
end)



game.Players.PlayerRemoving:connect(function(player)
	ds1:SetAsync(player.UserId, player.Level.Value)
	print(player.Name .. "has left. Saved" .. player.Level.Value .. "as Level")
	
end)


How do I stop it from looping?

5 comments

0

The most important part about a data store is that the server should decide when and what to save, it is very bad to use the changed event o save data as you have no control over it.

0

How should I do that then?

0

There is no point to constantly saving the value since you do it when they leave anyway.

0

Cabbler, you make a good point. I'll just have it save when they leave.

0

It is also good to use a small loop to save all of the players data but include a wait between each player save.