im making a Saving script and i keep getting the same error all the time and i dont know why, please help me Developers
Script
local DataSystem = {}
-- Services
local Players = game:GetService("Players")
-- Modules
local ProfileService = require(script.ProfileService)
local UserData = require(script.UserData)
-- Variables
local DataVersion = 01
local DataStoreKey = "playerData-"..tostring(DataVersion)
local GameProfileStore = ProfileService.GetProfileStore(DataStoreKey, UserData)
local Profiles = {}
function DataSystem:GetData(Player)
local Profile = Profiles[Player]
if Profile == nil then Player:Kick("Data Wasn't Found, Please Rejoin!") return end
return Profiles[Player].Data
end
function DataSystem:LoadProfile(Player)
local Key = "Player".. tostring(Player.UserId)
local Profile = GameProfileStore:LoadProfileAsync(Key, "ForceLoad")
print(Profile)
if Profile == nil then Player:Kick("Data Wasn't Found, Please Rejoin!") return end
Profile:Recocile()
Profile:ListenToRelease(function()
Profiles[Player] = nil
Player:Kick(ProfileService.ReleaseKickMessage)
end)
if Player:IsDecendantOf(Players) then
Profiles[Player] = Profile
LoadData(Player)
else
Profile:Release()
end
end
function DataSystem:RemoveProfile(Player)
local Profile = Profiles[Player]
if Profile == nil then warn(Player.Name.."'s Profile Is Nil") return end
Profile:Release()
end
function LoadData(Player)
local Profile = Profiles[Player]
if Profile == nil then warn(Player.Name.."'s Profile Is Nil") return end
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local Data = Profile.Data
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
cash.Value = Data.Cash
end
return DataSystem
it says you cant index nil with UserId, meaning that the âPlayerâ parameter is nil, check if your passing the player parameter when your calling the function
because you havenât put in the actual player instance using the DataSystem:LoadProfile
must be assuming you put the name of the player instead of the player instance
game.Players.Player1.Name is a string and
game.Players.Player1 is an instance
the string does not have UserId but the instance does
(never saw the function call to use that function)
i think i have connected it, let me send you the server script,
local Players = game:GetService("Players")
local DataSystem = require(script.Parent.ServerScripts.DataSystem)
function PlayerAdded(player)
DataSystem:LoadProfile(player)
local function CharacterAdded(character)
print(character)
end
CharacterAdded(player.Character or player.CharacterAdded.Wait())
player.CharacterAdded:Connect(CharacterAdded)
end
function PlayerRemoving(player)
DataSystem:RemoveProfile(player)
end
Players.PlayerAdded:Connect(PlayerAdded())
for _, player in pairs(Players:GetPlayers()) do
task.spawn(function()
PlayerAdded(player)
end)
end
Players.PlayerRemoving:Connect(PlayerRemoving)