Hello, for my game I made (by combining several youtube videos and a little, too little knowledge) a DataStore Script for my game. The script works well and the data is saved. However my script is basic and I wonder if you could give me some tips or more, to make this script more optimal. Thanks
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("Creatvurceslflfdfffdgfvfll")
game.Players.PlayerAdded:Connect(function(plr)
local creatures = Instance.new("Folder")
creatures.Name = "Creatures"
creatures.Parent = plr
local donation = Instance.new("BoolValue")
donation.Name = "Donator"
donation.Parent = plr
local data = SaveData:GetAsync("CreaturesData-"..plr.UserId)
local donator = SaveData:GetAsync(plr.UserId)
if data then
for _, creature in pairs(data) do
if workspace.Creatures:FindFirstChild(creature) then
local bool = Instance.new("BoolValue")
bool.Name = creature
bool.Parent = creatures
for i,v in pairs(game.Workspace.Creatures:GetChildren()) do
v.CanTouch = true
end
end
end
if donator then
donation.Value = donator
for i,v in pairs(game.Workspace.Creatures:GetChildren()) do
v.CanTouch = true
end
end
else
for i,v in pairs(game.Workspace.Creatures:GetChildren()) do
v.CanTouch = true
print("Nodata") -- If the script comes here it is because no Data has been found.
end
end
end)
--As soon as the player makes an important action, the data is saved
game.ReplicatedStorage.Nametag.OnServerEvent:Connect(function(plr)
wait(1)
local CreaturesName = {}
local DonatorSave = {}
for _, creature in pairs(plr.Creatures:GetChildren()) do
table.insert(CreaturesName, creature.Name)
end
table.insert(DonatorSave, plr.Donator.Value)
SaveData:SetAsync("CreaturesData-"..plr.UserId, CreaturesName)
SaveData:SetAsync(plr.UserId, DonatorSave)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local CreaturesName = {}
local DonatorSave = {}
for _, creature in pairs(plr.Creatures:GetChildren()) do
table.insert(CreaturesName, creature.Name)
end
table.insert(DonatorSave, plr.Donator.Value)
SaveData:SetAsync("CreaturesData-"..plr.UserId, CreaturesName)
SaveData:SetAsync(plr.UserId, DonatorSave)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local CreaturesName = {}
local DonatorSave = {}
for _, creature in pairs(plr.Creatures:GetChildren()) do
table.insert(CreaturesName, creature.Name)
end
table.insert(DonatorSave, plr.Donator.Value)
SaveData:SetAsync("CreaturesData-"..plr.UserId, CreaturesName)
SaveData:SetAsync(plr.UserId, DonatorSave)
end
end)