I am not very good at scripting DataStores, and I already failed. I am trying to save a string to a datastore, how can i manage?
--// Services
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
--// Variables
local DS = DSS:GetOrderedDataStore("Data7")
--// Function
local function OnPlayerAdded(player)
local plr_key = "id_"..player.UserId
local plr_key2 = "2id_"..player.UserId
local success, data = pcall(function()
return DS:GetAsync(plr_key)
end)
local success, data2 = pcall(function()
return DS:GetAsync(plr_key2)
end)
if data2 == nil then
DS:SetAsync(plr_key2, "")
end
local stats = Instance.new("Folder")
stats.Parent = player
stats.Name = "leaderstats"
local AeroCoins = Instance.new("IntValue")
AeroCoins.Parent = stats
AeroCoins.Name = "AeroCoins"
game.ReplicatedStorage.RewardEntity.OnServerEvent:Connect(function(Player, EntityName)
local success, result = pcall(function()
data2 = data2 .. EntityName .. ","
return DS:SetAsync(plr_key2, data2)
end)
end)
game.ReplicatedStorage.RewardCash.OnServerEvent:Connect(function(Player, Player2, Reward)
if Player == player then
AeroCoins.Value += Reward
end
end)
if success then
AeroCoins.Value = data or 0
end
game.ReplicatedStorage.Values.DoorNumber.Changed:Connect(function(Value)
AeroCoins.Value += math.random(1, 4)
end)
end
local function OnPlayerRemoving(player)
local plr_key = "id_"..player.UserId
local success, result = pcall(function()
return DS:SetAsync(plr_key, player.leaderstats.AeroCoins.Value)
end)
if not success then
warn(result)
end
end
local function OnServerShutdown()
for _, player in Players:GetPlayers() do
local plr_key = "id_"..player.UserId
local success, result = pcall(function()
return DS:SetAsync(plr_key, player.leaderstats.AeroCoins.Value)
end)
if not success then
warn(result)
end
end
end
--// Connections
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnServerShutdown)
--// AeroCoin Payment