Datastore2 causing lag

Hi, I am using DataStore2 as a lot of people seem to recommend it. I have set it up, it works fine but I have noticed that when the player dies, it lags then respawns the player 5 seconds later.

Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:WaitForChild("Storage"):WaitForChild("Modules")

local DataStore2 = require(1936396537)
local Settings = require(Modules["Datastores"]) 

Players.PlayerAdded:Connect(function(plr)
	for i,v in pairs(Settings) do
		local datastore = DataStore2(i,plr) 
		local Folder = v.Folder 
		
		if v.Folder ~= "Player" then
			if plr:findFirstChild(v.Folder) then
				Folder = plr[v.Folder]
			else
				local folder = Instance.new("Folder",plr) 
				folder.Name = v.Folder 
				Folder = folder 
			end
		end
		
		if v.Folder == "Player" then
			Folder = plr
		end
	
		local val = Instance.new(v.ValueType , Folder)
		val.Name = i 
		val.Value = v.Value 
		
		if datastore:Get() ~= nil then
			val.Value = datastore:Get() 
		end
		
		val.Changed:connect(function()
			datastore:Set(val.Value) 
		end)
	end
end)

Any help would be appriciated, thanks!

1 Like

Hmm, That’s strange maybe those deperecated stuff causing lag?

Change this

val.Changed:connect(function()
	datastore:Set(val.Value) 
end)

To this

val:GetPropertyChangedSignal("Value"):Connect(function()
	datastore:Set(val.Value) 
end)

Never use the Parent argument in the Instance.new() It is very SLOW and deprecated.

Change this

local val = Instance.new(v.ValueType , Folder)

To this

local val = Instance.new(v.ValueType)
val.Parent = Folder

Thanks for this, but it it still lagging when I respawn/

I’ve also tested your script it’s fine for me and not lagging at all could be your computer you might wanna switch to ProfileService…

There is also a plugin (I made which creates leaderstats with DataStores, Including DS,DS2,PS)
Maybe it can help you.

It isnt my computer, I get over 60 fps. Maybe it could be because of one of the scripts in the game? I am not sure which though.

I will try to use the same Datastore2 script in an empty baseplate game.

It turns out it doesn’t lag in an empty baseplate game, very weird. What can I do now then?

Thanks for the plugin, I really appriciate it, but my game is still lagging when I die

Do you have any other scripts?
I don’t know how would a Datastore has a connection between resetting it makes no sence.

No, but when I disable the datastore script it suddenly stops.

That’s weird can you try the script in a baseplate?
I’m running the code in Roblox studio and it’s perfectly fine (Game has 10K+ lines of script).

Nevermind, I found the cause.
Turns out there was a script in my settings menu that was causing the game to lag, and because I had the datastore script disabled it didnt run.

1 Like