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

[Solved]Why is my data store not retrieving the data correctly?

Asked by 5 years ago
Edited 5 years ago

So I have a script which saves player data into a table here is the script:

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)
        end
    end
end

When a player touches the tycoon door this fires however it is kicking the player(me) every time here is that script:

local mainDataStore = game:GetService("DataStoreService"):GetDataStore("exampledatastore")
script.Parent.Touched:Connect(function(hit)
    if script.Parent.CanCollide then
        if hit.Parent:FindFirstChild("Humanoid") then
            -- more irrelevant code here
            local mainDataTable 
            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
            local specialKey = "User_"..plr.UserId


            local success, message = pcall(function()

                mainDataTable = mainDataStore:GetAync(specialKey)

            end)

            if success then
                -- some code here
            else

                print("Player Data Failed To Load Successfully") -- this stuff is what is happening
                plr:FindFirstChild("CanSave").Value = false
                plr:Kick("Your Data Failed To Load Successfully Please Rejoin")
            end
        end
    end
end)

Could someone help me figure out what is wrong? Thanks! Note: The game saves ok. It is the loading that is the problem

0
Can you add the variables to the script? PlaasBoer 275 — 5y
0
Ok the ones you need are there I think User#21908 42 — 5y
0
Async was spelled wrong User#21908 42 — 5y

Answer this question