local data = game:GetService("DataStoreService"):GetDataStore("gamedata_112NSDNFD")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "GameData"
leaderstats.Parent = plr
local XP = Instance.new("IntValue")
XP.Name = "Experience"
XP.Value = 0
XP.Parent = leaderstats
local UCFLevel = Instance.new("IntValue")
UCFLevel.Name = "CardLevel"
UCFLevel.Parent = leaderstats
UCFLevel.Value = 0
local Overseer = Instance.new("BoolValue")
Overseer.Name = "IsOverseer"
Overseer.Parent = leaderstats
local Division = Instance.new("StringValue")
Division.Name = "DivisionOverseer"
Division.Parent = Overseer
Division.Value = "N/A"
local ExperienceData
local cLvlData
local success, errormessage = pcall(function()
ExperienceData = data:GetAsync(plr.UserId.."-experience")
print("exp data")
cLvlData = data:GetAsync(plr.UserId.."-cLvl")
print("card data")
end)
if success then
plr.GameData.Experience.Value = ExperienceData
plr.GameData.CardLevel.Value = cLvlData
print("\nUCF DATABASE\nPlayer "..plr.Name.."'s data has been loaded.")
else
warn("\nUCF DATABASE\nPlayer "..plr.Name.."'s data could not be loaded. Error: "..errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
data:SetAsync(plr.UserId.."-experience", plr.GameData.Experience.Value)
data:SetAsync(plr.UserId.."-cLvl", plr.GameData.CardLevel.Value)
end)
if success then
print("UCF DATABASE\nPlayer "..plr.Name.."'s data has been saved.")
else
warn("UCF DATABASE\nPlayer "..plr.Name.."'s data could not be saved. Error: "..errormessage)
end
end)
Scripts wont run under the player instance, except if they’re parented inside the player backpack(and LocalScripts only run in PlayerGui or PlayerScripts).
Personally I consider putting scripts under backpack a bad practice(except if it’s related to a tool), the code snippet provided above can run inside ServerScriptService with a few changes applied:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.GameData.Experience.Changed:Connect(function(new)
print("UCF DATA CHANGE\nData changed: Experience\nNew data: "..new)
end)
player.GameData.CardLevel.Changed:Connect(function(new)
print("UCF DATA CHANGE\nData changed: Card Level\nNew data: "..new)
end)
end)