Hey Roblox devs. I’m making a datastore system in my game, but it doesn’t seem to be working for some reason. It’s not giving me any errors, so I don’t really understand why it wouldn’t work…
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("IntValue", leaderstats)
Coins.Name = "Coins"
local XP = Instance.new("IntValue", leaderstats)
XP.Name = "XP"
local Level = Instance.new("IntValue", player)
Level.Name = "Level"
local Rank = Instance.new("IntValue", 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("IntValue", GunLevels)
BlackoutLevel.Name = "BlackoutLevel"
local MP7Level = Instance.new("IntValue", GunLevels)
MP7Level.Name = "MP7Level"
local campaignLevel = Instance.new("IntValue", player)
campaignLevel.Name = "campaignLevel"
local CurrentObjective = Instance.new("IntValue", player)
CurrentObjective.Name = "CurrentObjective"
local playerID = player.UserId
local data = nil
pcall(function()
data = PlayerData:GetAsync(playerID)
end)
if data~= nil then
Coins.Value = data
XP.Value = data
Level.Value = data
Rank.Value = data
BlackoutLevel.Value = data
MP7Level.Value = data
campaignLevel.Value = data
CurrentObjective.Value = data
print("og player")
else
Coins.Value = 0
XP.Value = 0
Level.Value = 0
Rank.Value = 0
BlackoutLevel.Value = 0
MP7Level.Value = 0
campaignLevel.Value = 0
CurrentObjective.Value = 0
print("new player to game")
end
end)
Players.PlayerRemoving:Connect(function(player)
local playerID = player.UserId
local Coins = player.leaderstats.Coins.Value
local XP = player.leaderstats.XP.Value
local Level = player.Level.Value
local Rank = player.Rank.Value
local BlackoutLevel = player.GunLevels.BlackoutLevel.Value
local MP7Level = player.GunLevels.MP7Level.Value
local campaignLevel = player.campaignLevel.Value
local CurrentObjective = player.CurrentObjective.Value
local data = {
Coins,
XP,
Level,
Rank,
BlackoutLevel,
MP7Level,
campaignLevel,
CurrentObjective,
}
PlayerData:SetAsync(playerID, data)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down")
end
end
wait(5)
end)
local playerID = player.UserId
local data = nil
pcall(function()
data = PlayerData:GetAsync(playerID)
end)
to this
local playerID = player.UserId
local data = nil
local success, err = pcall(function()
data = PlayerData:GetAsync(playerID)
end)
if success then print("got data") else print("failed to get data") end
put this in the local script, change the entire thing
local proximityPrompt = game.Workspace.click.ProximityPrompt
wait(1)
proximityPrompt.Triggered:Connect(function()
game:GetService("ReplicatedStorage"):FindFirstChild("RE"):FireServer() -- make a remoteevent in replicated storage called RE
end)
put a script in serverscriptservice, this will monitor the remote events
The problem is the way you save and load the data.
You are saving the data as an array, which is fine but not optimal. However when loading the data you set the value of each value to the same variable? That does not make sense.
Instead of saving the data as array you can use a dictionary:
local DataToSave = {
Coins = Coins.Value,
Points = Points.Value,
-- and so on
}
When loading you can then just index the value by the key:
Coins.Value = data.Coins
Points.Value = data.Points
-- and so on