Hi, I have a datatore script which works fine, but I noticed when it is enabled, it lags whenver i reset, and when it is disabled, it doesn’t lag.
The script:
local DataStoreService = game:GetService("DataStoreService")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Modules = ServerScriptService:WaitForChild("Modules")
local DataModule = require(Modules:FindFirstChild("DataModule"))
local PlayerStores = DataStoreService:GetDataStore("MainStoresRedo")
local ExtraStores = DataStoreService:GetDataStore("ExtraStoresRedo")
local HoldStores = DataStoreService:GetDataStore("BallHoldStoresRedo")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local LoadData = RemoteEvents["LoadData"]
Players.PlayerAdded:Connect(function(plr)
local Folder = Instance.new("Folder" , plr)
Folder.Name = "Settings"
local FOV = Instance.new("IntValue" , Folder)
FOV.Name = "FOV"
local Time = Instance.new("IntValue" , Folder)
Time.Name = "Time"
local Brightness = Instance.new("IntValue" , Folder)
Brightness.Name = "Brightness"
local Cooldown = Instance.new("IntValue" , Folder)
Cooldown.Name = "Cooldown"
local Seconds = Instance.new("IntValue" , Folder)
Seconds.Name = "Seconds"
local HoldType = Instance.new("StringValue" , Folder)
HoldType.Name = "Ball_Hold"
local key = plr.UserId .. "-stores"
local data
local data1
local data2
local success, err = pcall(function()
data = DataModule.LoadData(key , PlayerStores)
data1 = DataModule.LoadData(key , ExtraStores)
data2 = DataModule.LoadData(key , HoldStores)
end)
if success then
if data then
FOV.Value = (data['FOV']);
Time.Value = (data['Time']);
Brightness.Value = (data['Brightness']);
LoadData:FireClient(plr)
elseif data1 then
Cooldown.Value = (data['Cooldown']);
Seconds.Value = (data['Seconds']);
LoadData:FireClient(plr)
elseif data2 then
HoldType.Value = (data2)
print(data2)
LoadData:FireClient(plr)
elseif not data then
FOV.Value = 70
Time.Value = 12
Brightness.Value = 0
LoadData:FireClient(plr)
elseif not data1 then
Cooldown.Value = 0.075
Seconds.Value = 0
LoadData:FireClient(plr)
elseif not data2 then
HoldType.Value = "Default"
print(HoldType.Value)
LoadData:FireClient(plr)
end
elseif not success then
warn("Datastores failed, with error: " .. err)
end
end)
local BlacklistMain = {"Seconds" , "Cooldown"}
local BlacklistBall = {"Ball_Hold"}
local function create_tableMain(player)
local player_stats = {}
for _, stat in pairs(player.Settings:GetChildren()) do
if not table.find(BlacklistMain , stat.Name) and not table.find(BlacklistBall , stat.Name) then
player_stats[stat.Name] = stat.Value
end
end
return player_stats
end
local function create_tableOthers(player)
local player_stats = {}
for _, stat in pairs(player.Settings:GetChildren()) do
if table.find(BlacklistMain , stat.Name) then
player_stats[stat.Name] = stat.Value
end
end
return player_stats
end
local function ball_hold(player)
return player:WaitForChild("Settings")["Ball_Hold"].Value
end
Players.PlayerRemoving:Connect(function(plr)
local main_stats = create_tableMain(plr)
local other_stats = create_tableOthers(plr)
local ball_hold = ball_hold(plr)
local key = plr.UserId .. "-stores"
local success, err = pcall(function()
DataModule.SaveData(key , main_stats , PlayerStores)
DataModule.SaveData(key , other_stats , ExtraStores)
DataModule.SaveData(key , ball_hold , HoldStores)
end)
if not success then
warn('There was a DataStore error: ' .. err)
end
end)
Any help would be appriciated, thanks!