Data saving script erroring on test player 1 leaving and test player 2 joining then leaving?
In this code i get the error file -2 does not exist in the module.
non module code:
01 | local players = game.Players |
02 | local dataservice = game:GetService( "DataStoreService" ) |
03 | local playerstore = dataservice:GetDataStore( "PlayerDataStore" ) |
04 | local savemodule = require(game.ServerScriptService.PlayerData) |
06 | function SaveData(player,id,cpos) |
08 | cpos = Vector 3. new( 0 , 0 , 0 ) |
10 | local moduleindex = nil |
11 | for i = 1 ,#savemodule do |
12 | local playerssavestore = savemodule [ i ] |
13 | if (id = = playerssavestore [ 1 ] ) then |
14 | moduleindex = playerssavestore |
16 | if (moduleindex ~ = nil ) then |
17 | moduleindex [ 3 ] = Vector 3. new(cpos) |
18 | playerstore:SetAsync(id,moduleindex) |
21 | error ( "File " ..id.. " does not exist in the module." ) |
26 | local playerssave = playerstore:GetAsync(id) |
28 | if (playerssave ~ = nil ) then |
29 | savemodule.profilecreate(playerssave) |
31 | local newprofile = { id, 200 ,Vector 3. new( 0 , 0 , 0 ) } |
32 | savemodule.profilecreate(newprofile) |
35 | function PlayerJoined(player) |
36 | local success,message = pcall ( function () GetData(player.UserId) end ) |
40 | print (player.Name.. " has joined" ) |
41 | local id = player.UserId |
42 | local playerfindex = nil |
43 | for i = 1 ,#savemodule do |
44 | local infospot = savemodule [ i ] |
45 | if (infospot [ 1 ] = = player.UserId) then |
46 | playerfindex = savemodule [ i ] |
52 | if (player.Character ~ = nil ) then |
53 | local character = player.Character |
54 | local humanoid = character:FindFirstChildOfClass( "Humanoid" ) |
55 | local root = character:FindFirstChild( "HumanoidRootPart" ) |
56 | if (humanoid.Health > 0 ) then |
60 | local success,message = pcall ( function ()SaveData(player,id,cpos) end ) |
64 | print (player.Name.. "'s data was auto saved" ) |
66 | until playerfindex = = nil or player.Parent = = nil |
70 | function PlayerLeft(player) |
72 | local id = player.UserId |
73 | if (player.Character ~ = nil ) then |
74 | local character = player.Character |
75 | local humanoid = character:FindFirstChildOfClass( "Humanoid" ) |
76 | local root = character:FindFirstChild( "HumanoidRootPart" ) |
77 | if (humanoid.Health > 0 ) then |
81 | local success,message = pcall ( function ()SaveData(player,id,cpos) end ) |
85 | savemodule.profileremove(player.UserId) |
86 | print (player.Name.. " has left" ) |
89 | game.Players.PlayerAdded:Connect(PlayerJoined) |
90 | game.Players.PlayerRemoving:Connect(PlayerLeft) |
module code:
03 | function playerinfo.profilecreate(playersinfo) |
04 | local ownprop = playersinfo |
05 | local newplayer = ownprop |
06 | table.insert(playerinfo,#playerinfo + 1 ,newplayer) |
08 | function playerinfo.modgold(id,amount) |
09 | for i = 1 ,#playerinfo do |
10 | local playersprofile = playerinfo [ i ] |
11 | if (playersprofile [ 1 ] = = id) then |
12 | playersprofile [ 2 ] = playersprofile [ 2 ] + amount |
16 | function playerinfo.profileremove(id) |
17 | for i = 1 ,#playerinfo do |
18 | local playersprofile = playerinfo [ i ] |
19 | if (playersprofile [ 1 ] = = id) then |
20 | table.remove(playerinfo,i) |