DataStoreHelp.rbxl (35.8 KB)
My code is intended to save and load values to a datastore such as Coins, Kills, Deaths, Chat Prefixes, Donations, etc. While it works, I believe there would be a better way to optimize it, but I do not know how.
Any feedback or help would be greatly appreciated! This is my first large-scale project that has needed datastores, so I’m not too great with them.
Datastore Script (In ServerScriptService)
local DataStoreService = game:GetService('DataStoreService')
local playerKillsValue = DataStoreService:GetDataStore('playerLifeTimeKills')
local playerDeathsValue = DataStoreService:GetDataStore('playerLifeTimeDeaths')
local playerCoinsValue = DataStoreService:GetDataStore('playerCoins')
local playerPrefixValue = DataStoreService:GetDataStore('playerPrefix')
local playerDonationAmount = DataStoreService:GetDataStore('playerDonations')
game.Players.PlayerAdded:Connect(function(player)
local Kills = Instance.new('NumberValue')
Kills.Name = 'Kills'
Kills.Parent = player:WaitForChild('playerVariables')
local Deaths = Instance.new('NumberValue')
Deaths.Name = 'Deaths'
Deaths.Parent = player:WaitForChild('playerVariables')
local Coins = Instance.new('NumberValue')
Coins.Name = 'Coins'
Coins.Parent = player:WaitForChild('playerVariables')
local Prefix = Instance.new('StringValue')
Prefix.Name = 'Prefix'
Prefix.Parent = player:WaitForChild('playerVariables')
local Donations = Instance.new('NumberValue')
Donations.Name = 'Donations'
Donations.Parent = player:WaitForChild('playerVariables')
local data
local data2
local data3
local data4
local data5
local success, failure = pcall(function()
data = playerKillsValue:GetAsync(player.UserId.. ":Kills")
data2 = playerDeathsValue:GetAsync(player.UserId.. ":Deaths")
data3 = playerCoinsValue:GetAsync(player.UserId.. ":Coins")
data4 = playerPrefixValue:GetAsync(player.UserId.. ":Prefix")
data5 = playerDonationAmount:GetAsync(player.UserId.. ":Donations")
end)
if success then
print('Sucess loading data values for ' .. player.Name)
Kills.Value = data
Deaths.Value = data2
Coins.Value = data3
Prefix.Value = 'None'
Prefix.Value = data4
Donations.Value = data5
else
print('Error retriving data for ' .. player.Name)
warn(failure)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, failure = pcall(function()
playerKillsValue:SetAsync(player.UserId.. ':Kills', player:WaitForChild('playerVariables'):WaitForChild('Kills').Value)
playerDeathsValue:SetAsync(player.UserId.. ':Deaths', player:WaitForChild('playerVariables'):WaitForChild('Deaths').Value)
playerCoinsValue:SetAsync(player.UserId.. ':Coins', player:WaitForChild('playerVariables'):WaitForChild('Coins').Value)
playerPrefixValue:SetAsync(player.UserId.. ':Prefix', player:WaitForChild('playerVariables'):WaitForChild('Prefix').Value)
playerDonationAmount:SetAsync(player.UserId.. ':Donations', player:WaitForChild('playerVariables'):WaitForChild('Donations').Value)
end)
if success then
print("Saved data values for " .. player.Name)
else
print('data values for ' .. player.Name .. ' were not saved sucessfully')
warn(failure)
end
end)