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

Data saving script erroring on test player 1 leaving and test player 2 joining then leaving?

Asked by 5 years ago

In this code i get the error file -2 does not exist in the module.

non module code:

local players = game.Players
local dataservice = game:GetService("DataStoreService")
local playerstore = dataservice:GetDataStore("PlayerDataStore")
local savemodule = require(game.ServerScriptService.PlayerData)
local AUTOSAVEINT = 60
function SaveData(player,id,cpos)
    if(cpos == nil)then
        cpos = Vector3.new(0,0,0)
    end
    local moduleindex = nil
        for i = 1,#savemodule do
            local playerssavestore = savemodule[i]
        if(id == playerssavestore[1])then
            moduleindex = playerssavestore
        end
        if(moduleindex ~= nil)then
            moduleindex[3] = Vector3.new(cpos)
            playerstore:SetAsync(id,moduleindex)
            print(id)
        else
            error("File "..id.." does not exist in the module.")
        end
    end
end
function GetData(id)
    local playerssave = playerstore:GetAsync(id)
    print(id)
    if(playerssave ~= nil)then
    savemodule.profilecreate(playerssave)
    else
        local newprofile = {id,200,Vector3.new(0,0,0)}
        savemodule.profilecreate(newprofile)
    end
end
function PlayerJoined(player)
    local success,message = pcall(function() GetData(player.UserId) end)
    if not success then
        error(message)
    else
        print(player.Name.." has joined")
        local id = player.UserId
        local playerfindex = nil
        for i = 1,#savemodule do
            local infospot = savemodule[i]
            if(infospot[1] == player.UserId)then
                playerfindex = savemodule[i]
            end
        end
        repeat
            wait(AUTOSAVEINT)
            local cpos
    if(player.Character ~= nil)then
        local character = player.Character
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        local root = character:FindFirstChild("HumanoidRootPart")
        if(humanoid.Health > 0)then
    cpos = root.Position
    end
    end
            local success,message = pcall(function()SaveData(player,id,cpos)end)
    if not success then
        error(message)
    else
        print(player.Name.."'s data was auto saved")
    end
        until playerfindex == nil or player.Parent == nil
    end

end
function PlayerLeft(player)
    local cpos
    local id = player.UserId
    if(player.Character ~= nil)then
        local character = player.Character
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        local root = character:FindFirstChild("HumanoidRootPart")
        if(humanoid.Health > 0)then
    cpos = root.Position
    end
    end
    local success,message = pcall(function()SaveData(player,id,cpos)end)
    if not success then
        error(message)
    else
        savemodule.profileremove(player.UserId)
        print(player.Name.." has left")
    end
end
game.Players.PlayerAdded:Connect(PlayerJoined)
game.Players.PlayerRemoving:Connect(PlayerLeft)

module code:

local playerinfo = {}

function playerinfo.profilecreate(playersinfo)
    local ownprop = playersinfo
    local newplayer = ownprop
    table.insert(playerinfo,#playerinfo + 1,newplayer)
end
function playerinfo.modgold(id,amount)
    for i = 1,#playerinfo do
        local playersprofile = playerinfo[i]
        if(playersprofile[1] == id)then
            playersprofile[2] = playersprofile[2] + amount
        end
    end
end
function playerinfo.profileremove(id)
    for i = 1,#playerinfo do
        local playersprofile = playerinfo[i]
        if(playersprofile[1] == id)then
            table.remove(playerinfo,i)
        end
    end
end
return playerinfo
0
I just realized it isnt proper UTF :/ 129Steve129 7 — 5y

Answer this question