Hey Roblox devs. I have a datastore system in my game, and it doesn’t seem to work…I’ve tried looking at tutorials, reading things about datastores, and I tried to apply everything I know into this but I tested it and it doesn’t save any data. Not sure if it’s a problem with how it saves the data, or how it loads the data. Here is my script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local MultiplayerData = DataStoreService:GetDataStore("MultiplayerData")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Coins = Instance.new("NumberValue", leaderstats)
Coins.Name = "Coins"
Coins.Value = 25
local XP = Instance.new("NumberValue", leaderstats)
XP.Name = "XP"
local Level = Instance.new("NumberValue", player)
Level.Name = "Level"
local Rank = Instance.new("NumberValue", player)
Rank.Name = "Rank"
local GunsUnlocked = Instance.new("Folder", player)
GunsUnlocked.Name = "GunsUnlocked"
--add each unlockable gun as a bool value
local GunLevels = Instance.new("Folder", player)
GunLevels.Name = "GunLevels"
local BlackoutLevel = Instance.new("NumberValue", GunLevels)
BlackoutLevel.Name = "BlackoutLevel"
local MP7Level = Instance.new("NumberValue", GunLevels)
MP7Level.Name = "MP7Level"
local campaignLevel = Instance.new("NumberValue", player)
campaignLevel.Name = "campaignLevel"
local CurrentObjective = Instance.new("NumberValue", player)
CurrentObjective.Name = "CurrentObjective"
local playerID = player.UserId
local data
local success, errormessage = pcall(function()
data = PlayerData:GetAsync(playerID)
end)
if success then
Coins.Value = data
XP.Value = data
Level.Value = data
Rank.Value = data
BlackoutLevel.Value = data
campaignLevel.Value = data
CurrentObjective.Value = data
print("loaded data")
end
end)
Players.PlayerRemoving:Connect(function(player)
local playerID = player.UserId
local data = {
Coins = player.leaderstats.Coins.Value;
XP = player.leaderstats.XP.Value;
Level = player.Level.Value;
Rank = player.Rank.Value;
BlackoutLevel = player.GunLevels.BlackoutLevel.Value;
MP7Level = player.GunLevels.MP7Level.Value;
campaignLevel = player.campaignLevel.Value;
CurrentObjective = player.CurrentObjective.Value;
}
local success, errormessage = pcall(function()
PlayerData:SetAsync(playerID, data)
end)
if success then
print("saved data")
else
print("data didn't save")
warn(errormessage)
end
end)
I honestly have no clue how to fix my errors. I’m completely lost. The DevForum is my last resort, so I would appreciate it if someone could let me know what it wrong with my script.