Players.PlayerAdded:Connect(function(plr)
local Data = table.clone(DataFormat)
local success, err = pcall(function()
Data = DataStore:GetAsync(plr.UserId)
end)
if Data then
LoadData(plr, Data)
Gamepasses(plr)
Multipliers(plr)
AdditionalInfo(plr)
else
Data = table.clone(DataFormat)
LoadData(plr, Data)
Gamepasses(plr)
Multipliers(plr)
AdditionalInfo(plr)
warn("Data didn't load.")
end
plr.CharacterAdded:Connect(function(char)
Addtag(plr)
plr.Character.Humanoid.WalkSpeed = 25
game.ReplicatedStorage.Trails.AddTrailServer:Fire(plr)
end)
end)
This is my code and I have a problem.
Whenever a new player joins the game with no saved data, it loads data from the previous player with data. I have looked at this for a while and can’t see why and I feel like it is very oblivious. Any help will be greatly appreciated.
Players.PlayerAdded:Connect(function(plr)
local Data = nil
local success, err = pcall(function()
Data = DataStore:GetAsync(plr.UserId)
end)
if Data ~= nil then
LoadData(plr, Data)
Gamepasses(plr)
Multipliers(plr)
AdditionalInfo(plr)
else
Data = table.clone(DataFormat)
LoadData(plr, Data)
Gamepasses(plr)
Multipliers(plr)
AdditionalInfo(plr)
warn("Data didn't load.")
end
plr.CharacterAdded:Connect(function(char)
Addtag(plr)
plr.Character.Humanoid.WalkSpeed = 25
game.ReplicatedStorage.Trails.AddTrailServer:Fire(plr)
end)
end)
local function LoadData(plr : Player, data)
local NewDataFormat = table.clone(DataFormat)
for i, v in pairs(NewDataFormat) do
local Folder = Instance.new("Folder", plr)
Folder.Name = i
for a,b in pairs(v) do
if typeof(b) == "boolean" then
local Value = Instance.new("BoolValue", Folder)
Value.Name = a
Value.Value = data[i][a] or NewDataFormat[i][a]
elseif typeof(b) == "string" then
local Value = Instance.new("StringValue", Folder)
Value.Name = a
Value.Value = data[i][a] or NewDataFormat[i][a]
elseif typeof(b) == "number" then
local Value = Instance.new("IntValue", Folder)
Value.Name = a
Value.Value = data[i][a] or NewDataFormat[i][a]
end
end
end
print(data)
end