Error Messages:
Game Settings:
Code:
-- // Services
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")
local RepFirst = game:GetService("ReplicatedFirst")
local ServerScriptService = game:GetService("ServerScriptService")
-- // Dependencies
local Dependencies = ServerScriptService:WaitForChild("Dependencies")
local DataStore2 = require(Dependencies.MainModule)
local Settings = require(RepFirst:WaitForChild("DataStoreItems"))
local function OnPlayerAdded(plr: Player) -- // Datastore, creation of obj.
task.spawn(function()
for i, v in pairs(Settings) do
if i == "GlobalDuraUsage" or i == "Stamina" or i == "StaminaRecover" then continue end
local DataStore = DataStore2(i, plr)
local Where = v.Where
--// DataSaving Location
if Where ~= "Player" then
if plr:FindFirstChild(v.Where) then
Where = plr[Where]
else
local DataFolder = Instance.new("Folder", plr)
DataFolder.Name = v.Where
Where = DataFolder
end
else
Where = plr
end
--// Creates the value
local Value = Instance.new(v.What, Where)
Value.Name = i
Value.Value = v.Value
--// Loading Handler
if DataStore:Get() ~= nil then
Value.Value = DataStore:Get()
end
--// Saving Handler
Value.Changed:Connect(function()
DataStore:Set(Value.Value)
end)
end
-- non saving stats
local Statistics = plr:FindFirstChild("Statistics")
local Stamina = Instance.new("NumberValue", Statistics)
Stamina.Name = "Stamina"
Stamina.Value = Settings.Stamina.Value
local Recover = Instance.new("NumberValue", Statistics)
Recover.Name = "Recover"
Recover.Value = Settings.StaminaRecover.Value
local ArmorPoints = Instance.new("NumberValue", Statistics)
ArmorPoints.Name = "ArmorPoints"
ArmorPoints.Value = 0
local MaxStamina = Stamina.Value
while true do
while Stamina.Value < MaxStamina do
local REGEN_RATE = 1/50 * Recover.Value -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 0.1 * Recover.Value -- Wait this long between each regeneration step
local dt = task.wait(REGEN_STEP)
local dh = dt*REGEN_RATE*MaxStamina
Stamina.Value = math.min(Stamina.Value + dh, MaxStamina)
end
Stamina.Changed:Wait()
end
end)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
The issue happens every couple of play-tests randomly, what could be the problem?
Won’t save player data, but will load all data from previous sessions, just wont save.