Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can i make a table that holds pets you hatched and then saves it once they leave?

Tables are pretty complicated. I have no scripts for tables yet so this is the script that displays the items you got, works fine but how do i then make it add that to a table

This is just the display how can i make it do the stuff though.

--hatch
game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
	local player = game.Players.LocalPlayer
	player.PlayerGui.PetsUI.TextLabel.Text = pet.Name .." Hatched!"
	player.PlayerGui.PetsUI.TextLabel.Visible = true
	
	wait (3.5)
	player.PlayerGui.PetsUI.TextLabel.Visible = false
end)

1 answer

You should be able to use t.insert to insert the pet into a table and use DataStoreService to store the table in a database. DataStore Example:

local DataStore = game:GetService("DataStore")
local Players = game:GetService("Players")
local visitStore = DataStore:GetDataStore("visits")
Players.PlayerAdded:Connect(function(plr)
	-- load
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local visits = Instance.new("IntValue")
	visits.Value = DataStore:GetAsync(plr.UserId)  + 1 or 1
	visits.Name = "Visits"
	visits.Parent = leaderstats
	leaderstats.Parent = plr
end)
Players.PlayerRemoving:Connect(function(plr)
	-- save
	DataStore:SetAsync(plr.UserId, plr.leaderstats.Visits.Value)
end)

I think that's a bad example because you probably want IncrementAsync instead of GetAsync and SetAsync, but that only works for integers and DataStores can hold things other than integers. You should be able to load the pets stored in the database with GetAsync and store the results into a table, and store the table into the database when the player is done playing.

This discussion is preserved as it was. New answers, comments, and votes are closed.