Skip to content

Instantly share code, notes, and snippets.

@RobertCodez
Last active April 14, 2023 14:18
Show Gist options
  • Save RobertCodez/9253a1fbe576b2cdf6e647ea0adbc640 to your computer and use it in GitHub Desktop.
Save RobertCodez/9253a1fbe576b2cdf6e647ea0adbc640 to your computer and use it in GitHub Desktop.
Object Oriented Datastore
local myData = game:GetService("DataStoreService"):GetDataStore("yeah")
game.Players.PlayerAdded:Connect(function(plr)
local ChickensOwned = Instance.new("Folder", plr)
ChickensOwned.Name = "ChickensOwned"
pcall(function()
local data = myData:GetAsync(plr.UserId)
for _,v in pairs(data) do
local chicken = Instance.new("StringValue", ChickensOwned)
chicken.Name = "Chicken"
local Damage = Instance.new("IntValue", chicken)
Damage.Name = "Damage"
Damage.Value = v[1]
local Health = Instance.new("IntValue", chicken)
Health.Name = "Health"
Health.Value = v[2]
local Lifesteal = Instance.new("IntValue", chicken)
Lifesteal.Name = "Lifesteal"
Lifesteal.Value = v[3]
local AttackSpeed = Instance.new("IntValue", chicken)
AttackSpeed.Name = "AttackSpeed"
AttackSpeed.Value = v[4]
local ID = Instance.new("IntValue", chicken)
ID.Name = "ID"
ID.Value = v[5]
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local dataDictionary = {}
for index,v in pairs(plr.ChickensOwned:GetChildren()) do
dataDictionary["Chicken"..index] = index
dataDictionary["Chicken"..index] = {}
table.insert(dataDictionary["Chicken"..index], v.Damage.Value)
table.insert(dataDictionary["Chicken"..index], v.Health.Value)
table.insert(dataDictionary["Chicken"..index], v.Lifesteal.Value)
table.insert(dataDictionary["Chicken"..index], v.AttackSpeed.Value)
table.insert(dataDictionary["Chicken"..index], v.ID.Value)
end
myData:SetAsync(plr.UserId, dataDictionary)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment