Basically there is a bug within my game where the players data loads infinitely and I am not sure why. At first I thought I was getting the data too many times but it stops loading the on the first value I try to get… Sometimes it works fine, other times it takes forever. It seems to be when a laggy player joins the server first but I’m not completely sure.
I’ve tried to shorten the code as much as I can for this post. The actual script has a lot more in it but I removed it as it wasn’t necessary for this.
Anyone know how I can stop this issue from occurring?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
local MaxLevel = 100
local MaxGold = 1000
DataStore2.Combine(
"StatData1",
"Level",
"AvilablePoints",
"Strength",
"Durability",
"Stamina",
"Gold",
"MaxExp",
"Exp",
"InstantSpin",
"StoredPower"
)
game.Players.PlayerAdded:Connect(function(player)
local LevelStore = DataStore2("Level", player)
local AvilablePointsStore = DataStore2("AvilablePoints", player)
local PointsStore = DataStore2("Points", player)
local StrengthStore = DataStore2("Strength", player)
local DurabilityStore = DataStore2("Durability", player)
local StaminaStore = DataStore2("Stamina", player)
local GoldStore = DataStore2("Gold", player)
local MaxExpStore = DataStore2("MaxExp", player)
local ExpStore = DataStore2("Exp", player)
local InstantSpinStore = DataStore2("InstantSpin", player)
local StoredPowerStore = DataStore2("StoredPower", player)
local Statistics = Instance.new("Folder")
Statistics.Name = "Statistics"
Statistics.Parent = player
local InstantSpin = Instance.new("BoolValue")
InstantSpin.Name = "InstantSpin"
InstantSpin.Value = InstantSpinStore:Get(false)
InstantSpin.Parent = Statistics
InstantSpinStore:OnUpdate(function(new)
InstantSpin.Value = new
end)
InstantSpin.Changed:Connect(function()
InstantSpinStore:Set(InstantSpin.Value)
end)
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = LevelStore:Get(1)
Level.Parent = Statistics
LevelStore:OnUpdate(function(new)
Level.Value = new
end)
local AvilablePoints = Instance.new("IntValue")
AvilablePoints.Name = "AvilablePoints"
AvilablePoints.Value = AvilablePointsStore:Get(3)
AvilablePoints.Parent = Statistics
AvilablePointsStore:OnUpdate(function(new)
AvilablePoints.Value = new
end)
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Value = PointsStore:Get(0)
Points.Parent = Statistics
PointsStore:OnUpdate(function(new)
Points.Value = new
end)
local Strength = Instance.new("IntValue")
Strength.Name = "Strength"
Strength.Value = StrengthStore:Get(1)
Strength.Parent = Statistics
StrengthStore:OnUpdate(function(new)
Strength.Value = new
end)
local Durability = Instance.new("IntValue")
Durability.Name = "Durability"
Durability.Value = DurabilityStore:Get(1)
Durability.Parent = Statistics
DurabilityStore:OnUpdate(function(new)
Durability.Value = new
end)
local Stamina = Instance.new("IntValue")
Stamina.Name = "Stamina"
Stamina.Value = StaminaStore:Get(1)
Stamina.Parent = Statistics
StaminaStore:OnUpdate(function(new)
Stamina.Value = new
end)
local Gold = Instance.new("IntValue")
Gold.Name = "Gold"
Gold.Value = GoldStore:Get(10)
Gold.Parent = Statistics
GoldStore:OnUpdate(function(new)
Gold.Value = new
end)
local MaxExp = Instance.new("IntValue")
MaxExp.Name = "MaxExp"
MaxExp.Value = Level.Value * 100
MaxExp.Parent = Statistics
local Exp = Instance.new("IntValue")
Exp.Name = "Exp"
Exp.Value = ExpStore:Get(0)
Exp.Parent = Statistics
ExpStore:OnUpdate(function(new)
Exp.Value = new
end)
Exp.Changed:Connect(function()
ExpStore:Set(Exp.Value)
end)
local StoredPower = Instance.new("StringValue")
StoredPower.Name = "StoredPower"
StoredPower.Value = StoredPowerStore:Get("None")
StoredPower.Parent = Statistics
StoredPowerStore:OnUpdate(function(new)
StoredPower.Value = new
end)
StoredPower.Changed:Connect(function()
StoredPowerStore:Set(StoredPower.Value)
end)
Level.Changed:Connect(function()
MaxExp.Value = Level.Value * 100
end)
Level.Changed:Connect(function()
LevelStore:Set(Level.Value)
AvilablePointsStore:Set(AvilablePoints.Value)
PointsStore:Set(Points.Value)
StrengthStore:Set(Strength.Value)
DurabilityStore:Set(Durability.Value)
StaminaStore:Set(Stamina.Value)
end)
Gold.Changed:Connect(function()
if Gold.Value > MaxGold and player.UserId ~= 84689107 then
Gold.Value = MaxGold
end
GoldStore:Set(Gold.Value)
end)
end)