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

Sandbox Tycoon Base saving (need help!!?!!)

Asked by 4 years ago
Edited 4 years ago

so basically i made a base saving script using an inventory method with _G["PlayerData"][Player.Name]["Inventory"][Item.Name] and when attemping to save those items locations on the base it isnt working. here is the script(atleast part of it anyways, if you need the whole script just let me know)

edittttttttttttttttttt---------------- the error output says 142 thats what it is in the script but in the part of the script here it is line 47

edit: here is the error message:

18:08:19.002 - SandBox Tycoon Testing auto-recovery file was created
Finished Loading. (x2)
  table: 0x49eb77cd126bd24f
  18:08:42.017 - ServerScriptService.DataStore:122: attempt to index local 'PlayerData' (a nil value)
18:08:42.018 - Stack Begin
18:08:42.018 - Script 'ServerScriptService.DataStore', Line 122 - upvalue SaveData
18:08:42.019 - Script 'ServerScriptService.DataStore', Line 142
18:08:42.020 - Stack End

    for _,Item in pairs(game.ReplicatedStorage.items:GetChildren()) do
        PlayerData.Inventory[Item.Name] = PlayerData.Inventory[Item.Name] or 0
    end

    PlayerData["BasePlateInfromation"] = PlayerData["BasePlateInfromation"] or {}

    --PlayerData["BasePlateInfromation"] = {} --ONLY to reset your baseplate data

    if PlayerData["BasePlateInfromation"] ~= {} then
        for _,Table in pairs(PlayerData["BasePlateInfromation"]) do
            local tycoon = GetTycoon(Player)
            local itemname = Table[1]
            local x = Table[2]
            local y = Table[3]
            local z = Table[4]
            local rx = Table[5]
            local ry = Table[6]
            local rz = Table[7]
            local realitem = game.ReplicatedStorage.items:FindFirstChild(itemname)
            if realitem then
                local newitem = realitem:Clone()
                newitem.Parent = tycoon
                newitem:SetPrimaryPartCFrame(CFrame.new(Vector3.new(x+tycoon.BasePlate.CFrame.p.X,y+tycoon.BasePlate.CFrame.p.Y,z+tycoon.BasePlate.CFrame.p.Z))*CFrame.Angles(math.rad(rx),math.rad(ry),math.rad(rz)))
                for i,v in pairs(newitem.Model:GetChildren()) do
                    if v.Name == "Colored" then
                        v.BrickColor = getteamfromplayer(Player).TeamColor
                    end
                    if v.ClassName == "Script" then
                        v.Disabled = false
                    end
                end
            end
        end
    end
    PlayerData["Cash"] = PlayerData["Cash"] or 100 --Start Balance  
    --PlayerData.Cash = 100 --ONLY to reset 

    local Tag = Instance.new("BoolValue")
    Tag.Name = "Dataloaded"
    Tag.Parent = Player 

    _G["PlayerData"][Player.Name] = PlayerData
end

local function SaveData(Player)
    local PlayerData = _G["PlayerData"][Player.Name]
    PlayerData.BasePlateInfromation = SavebasePlate(Player)
    DatData:UpdateAsync(Player.UserId, function(OldData)
        OldData = OldData or {}
        return PlayerData
    end)
end

local function PlayerAdded(Player)
    local Tycoon = SearchEmpty()
    if Tycoon then
        Tycoon.Owner.Value = Player
        LoadData(Player)
        wait()
        Player.CharacterAdded:connect(function(char)
            char.HumanoidRootPart.CFrame = Tycoon.BasePlate.CFrame*CFrame.new(0,20,0)
        end)
    end
end

local function PlayerRemoved(Player)
    SaveData()
    wait()
    local Tycoon = GetTycoon(Player)
    if Tycoon then
        Tycoon.Owner.Value = nil
        for i,v in pairs(Tycoon:GetChildren()) do
            if v.ClassName == "Model" then
                v:Destroy()
            end
        end
    end
end

function game.ReplicatedStorage.GetData.OnServerInvoke(Player)
    if Player:FindFirstChild("Dataloaded") then
        local Data = _G["PlayerData"][Player.Name]
        if Data then
            return Data
        end
    end
end


game.Players.PlayerAdded:connect(PlayerAdded)
game.Players.PlayerRemoving:connect(PlayerRemoved)

Answer this question