Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this not deleting the players stuff after they leave?

Asked by 5 years ago

Here is the code:

local savedTycoonsTable = {}

for i,v in pairs(game.Workspace.Tycoons:GetChildren()) do

    if v:IsA("Model") then

        savedTycoonsTable[v.Name] = v:Clone()

    end
end

game.Players.PlayerRemoving:Connect(function(plr)

    if plr.CanSave.Value then

        local specialKey = "User_"..plr.UserId
        local plrName = plr.Name
        local plrInfo = game.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)

            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

                until success or saveAttempts == maxAttemptsAllowed

            end
        end 
    end

    wait(30)

    local plrName = plr.Name
    local plrFolder = game.ServerStorage:FindFirstChild(plrName)
    local tycoonName = plrFolder.PlayerInfo.OwnedTycoon.Value
    local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
    local tycoonCopy = savedTycoonsTable[tycoon.Name]:Clone()

    wait()

    plrFolder:Destroy() -- it should be destroying this stuff I have no idea why its not
    tycoon:Destroy()

    wait()

    tycoonCopy.Parent = game.Workspace.Tycoons

end)

1 answer

Log in to vote
2
Answered by 5 years ago

Not exactly sure what the issue here is, everything looks fine.

A few notes:

  1. plrFolder is in ServerStorage meaning it's not visible in workspace

A few options:

  1. Add a spawn function to the for loop
  2. Move the for loop under the PlayerRemoving event

I don't see any issues in code and the loop is the issue standing out to me.

spawn(function()
    for i,v in pairs(game.Workspace.Tycoons:GetChildren()) do

        if v:IsA("Model") then

            savedTycoonsTable[v.Name] = v:Clone()

        end
    end
end)

Or move it under the script completely (after the last end)

Hopefully, this helped solve your issue.

Best of luck.

0
You helped in more ways than you can know. Thank you so much! User#21908 42 — 5y
0
Np BlackOrange3343 2676 — 5y
Ad

Answer this question