My question is that if a player rejoins a game again right away, how can I stop that from messing up their data saving from earlier/ messing up their data loading? Here is what the playeradded event looks like:
players.PlayerAdded:Connect(function(plr)
if serverStorage:FindFirstChild(plr.Name) then -- here is what I have attempted to fix what I think may be a problem because when a player leaves the players folder is not destroyed right away
serverStorage:FindFirstChild(plr.Name):Destroy()
wait()
end
local specialKey = "User_"..plr.UserId
local plrName = plr.Name
local stats = Instance.new("Folder")
stats.Parent = plr
stats.Name = "leaderstats"
local cash = Instance.new("StringValue")
cash.Parent = stats
cash.Name = "Cash"
local plrFolder = Instance.new("Folder")
plrFolder.Parent = serverStorage
plrFolder.Name = plr.Name
local plrInfo = Instance.new("Folder")
plrInfo.Parent = plrFolder
plrInfo.Name = "PlayerInfo"
local money = Instance.new("NumberValue")
money.Parent = plrInfo
money.Name = "Money"
local buildLevel = Instance.new("IntValue")
buildLevel.Parent = plrInfo
buildLevel.Name = "BuildLevel"
local ownsTycoon = Instance.new("BoolValue")
ownsTycoon.Parent = plrInfo
ownsTycoon.Name = "OwnsTycoon"
local ownedTycoon = Instance.new("StringValue")
ownedTycoon.Parent = plrInfo
ownedTycoon.Name = "OwnedTycoon"
local canSave = Instance.new("BoolValue")
canSave.Parent = plr
canSave.Name = "CanSave"
canSave.Value = true
end)
Here is the player removing event:
players.PlayerRemoving:Connect(function(plr)
if plr.CanSave.Value then
local specialKey = "User_"..plr.UserId
local plrName = plr.Name
local plrInfo = serverStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")
if plrInfo.OwnsTycoon.Value then
local tycoonName = plrInfo.OwnedTycoon.Value
local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
local moneyToSave = plrInfo.Money.Value
local buildLevelToSave = plrInfo.BuildLevel.Value
local purchasedItemsTable = getTableForDataStore(tycoon, plrInfo)
local mainTableToSave = {moneyToSave, buildLevelToSave, purchasedItemsTable}
local success, message = pcall(function()
mainDataStore:SetAsync(specialKey, mainTableToSave)
end)
tycoon:Destroy()
local tycoonCopy = savedTycoonsTable[tycoon.Name]:Clone()
tycoonCopy.Parent = game.Workspace.Tycoons
local runTycoonDoor = require(moduleScripts.RunTycoonDoor)
runTycoonDoor(tycoonCopy)
if success then
print(plrName.."'s data saved successfully")
else
print(plrName.."'s data failed to save successfully")
wait(15)
local maxAttemptsAllowed = 3
local saveAttempts = 0
repeat
saveAttempts = saveAttempts + 1
local success, message = pcall(function()
mainDataStore:SetAsync(specialKey, mainTableToSave)
end)
if success then
print(plrName.."'s data saved successfully on try "..saveAttempts)
else
print(plrName.."'s data failed to save successfully on try "..saveAttempts)
end
wait(15)
until success or saveAttempts == maxAttemptsAllowed
end
end
end
wait()
local plrFolder = serverStorage:FindFirstChild(plr.Name)
plrFolder:Destroy() -- here is where I finally get rid of it
end)
So how do I stop the old data from messing things up if the player rejoins the same server right away or another server right away when the data has not finished saving? By the way I do have a data saving loop. Like really. If a player rejoins another server right away even and the data did not save right away on this server then they start working on another tycoon and the data finally saves after a while I just think it could make things really bad really fast? And they wont see what their data was supposed to save last server when they join a new one immediately right? Please help!

3 comments
It takes like 10 whole seconds to join a roblox game.
I forgot there is a wait(15) in the repeat loop
I will edit my question