How would I save these dragon values?

So I am trying to think of a way I would save these number values and stringvalues of a players dragon info. Such as the level the exp so far and the customized name the dragon has.

Here is a screen shot of an example of a dragon stringvalue in a folder of all the contained dragon string values aka the info.

image

Let me know if I need to explain better!

The stuff I know is to just grab the info but I don’t know how to save the info.

Also these can be added or removed from the folder at any random time so uh I don’t know how I would tackle this.

1 Like

Sounds like you need a datastore, you can read more about it on the api wiki and on the community tutorials!

1 Like

@gertkeno is correct.

Would definitely use a datastore for this, you can simply SetAsync when the player leaves to save data, and GetAsync when the player joins to load the data.

2 Likes

I know that it can save values but I don’t know how to add the info in a datastore and save it

The post isn’t really helping.

local DS = game:GetService("DataStoreService"):GetGlobalDataStore()
-- ^ the data store for saving values
local HS = game:GetService("HttpService")
-- ^ used for JSONEncode (on tables) and JSONDecode
local function saveDragon(player, dragon)
  local data = {dragon.DragonLevel.Value, dragon.DragonName.Value, dragon.DragonExp.Value}
  data = HP:JSONEncode(data)
  local key = tostring(player.UserId).."Dragon 1"
  local success, msg = pcall(function()
    DS:SetAsync(key, data)
  end)
  if not success then print("-----------", msg) return false end
  return true
end
local function getDragon(player, dragon)
  local key = tostring(player.UserId).."Dragon 1"
  local data
  local success, msg = pcall(function()
    data = DS:GetAsync(key)
  end)
  if not success then print("-----------", msg) return false end
  local data = HP:JSONDecode(data)
  dragon.DragonLevel.Value = data[1]
  dragon.DragonName.Value = data[2]
  dragon.DragonExp.Value = data[3]
  return true
end

Ok great i will now try to put that in my script but asides from that apparently instead of creating it once the script is like creating it more then once. what its creating is the information that is going to be saved.

image

Like I even added a debounce.

I should tell you guys my script shouldn’t I

function DragonHandler:ConnectDragonToCharacter(player,Dragon,SlotPosition)
	local Character = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
	local PlayerDragons = player:WaitForChild("PlayerDragons")

	local DragonInfo = Dragon:WaitForChild("DragonInfo")

	local success, err = pcall(function()
		if Character:FindFirstChild(player.Name) then
			Character:FindFirstChild(player.Name):Destroy()
		end

		Dragon:SetPrimaryPartCFrame(HumanoidRootPart.CFrame)

		local BabyDragon = Dragon:WaitForChild("BabyDragon")

		local createDb = false

		if createDb == false then
			createDb = true
			local DragonStringValue = Instance.new("StringValue",PlayerDragons)
			DragonStringValue.Name = Dragon.Name

			local DragonNames = {
				"Lucy",
				"Alex",
				"James",
				"John",
				"Pro",
				"Gamer",
				"Dragon",
				"Miles",
				"Jenny",
				"Mack"
			}

			local DragonNameSV = Instance.new("StringValue")
			DragonNameSV.Name = "DragonName"
			DragonNameSV.Parent = DragonStringValue
			DragonNameSV.Value = DragonNames[math.random(1,#DragonNames)]

			local DragonLevelNV = Instance.new("NumberValue")
			DragonLevelNV.Parent = DragonStringValue
			DragonLevelNV.Name = "DragonLevel"

			local DragonExp = Instance.new("NumberValue")
			DragonExp.Parent = DragonStringValue
			DragonExp.Name = "DragonExp"
		end

		local CharacterAttachA1 = Instance.new("Attachment",HumanoidRootPart)
		CharacterAttachA1.Name = "CharacterAttachA1"
		CharacterAttachA1.Orientation = Vector3.new(0,-180,90)

		local CharacterAttach2A2 = Instance.new("Attachment",HumanoidRootPart)
		CharacterAttach2A2.Name = "CharacterAttach2A2"
		CharacterAttach2A2.Orientation = Vector3.new(-0, 178, 90)

		local DragonAttachment = Dragon.PrimaryPart.HBA
		local DragonAlignPosition = Dragon.PrimaryPart.AlignPosition
		local DragonAlignOrientation = Dragon.PrimaryPart.AlignOrientation

		DragonAlignPosition.MaxForce = 500000
		DragonAlignPosition.Responsiveness = 20
		DragonAlignOrientation.MaxTorque = 500000
		DragonAlignOrientation.Responsiveness = 20

		DragonAlignPosition.Attachment0 = DragonAttachment
		DragonAlignPosition.Attachment1 = CharacterAttachA1

		DragonAlignOrientation.Attachment0 = DragonAttachment
		DragonAlignOrientation.Attachment1 = CharacterAttach2A2

		Dragon.PrimaryPart.Position = HumanoidRootPart.Position + Vector3.new(0,2.5,0)

		Dragon.Parent = Character

		local DragonClone = script.Dragon:Clone()
		DragonClone.Parent = Dragon
		DragonClone.Disabled = false

		local Location = PlayerDragons:FindFirstChild(Dragon.Name)

		local DragonNameTag = Dragon.PrimaryPart:FindFirstChild("DragonNameTag")
		DragonNameTag.DragonName.Text = Location:WaitForChild("DragonName").Value
		DragonNameTag.DragonLevel.Text = Location:WaitForChild("DragonName").Value.."'s Level: "..Location:WaitForChild("DragonLevel").Value

		Location.DragonName.Changed:Connect(function()
			DragonNameTag.DragonName.Text = Location:WaitForChild("DragonName").Value
			DragonNameTag.DragonLevel.Text = Location:WaitForChild("DragonName").Value.."'s Level: "..Location:WaitForChild("DragonLevel").Value
		end)

		DragonHandler:CheckDragonAge(Dragon,SlotPosition,CharacterAttachA1,CharacterAttach2A2)
		
		createDb = false
	end)
end

For the saving stuff is in the main script which calls the functions from my dragonhandler module. I need to figure this out so that I can easily run the saving functions without it getting confused and stuff like that.

you set createDb to false and then check if it’s false right after that’s not a working debounce.

Right after it turns to true so that I doesn’t run more then once

Trust me your debounce isn’t doing anything.

local debounce = false
function x()
  if debounce then return else debounce = true end
  print("waiting")  
  wait(1)
  print("done")
  debounce = false
end

The only way I would call this function twice is in an event or something.

coroutine.wrap(x)()
wait(0.1)
x()

Your debounce looks something like this:

function x()
  local debounce = false
  if debounce then return else debounce = true end
  print("waiting")  
  wait(1)
  print("done")
  debounce = false
end

In this case, debounce is always false when the if statement checks it.

1 Like

I will try this later I am busy rn.

there’s an error on getting the data
it says argument 1 missing or nil

Btw the Dragon 1 info will have to be added to the folder to save it right. But what if the player doesn’t have the dragon? It wouldn’t be added in the folder and your data store makes me save those values and it has to be existent there. So lets say player 1 has these dragons: Dragon 2, Dragon 3 and Dragon 5. How would I save those.

I know how to save everything in a folder that can be random items. But don’t know how to save the random items children. So maybe I should make sepperate folders for the dragons name string values and exp values.

you use HttpService:JSONEncode(table) to turn a table into a string and then you save the string. Then you can use HttpService:JSONDecode(string) on the saved string and get the table back. Lets say you do

local GDS = game:GetService("DataStoreService"):GetGlobalDataStore()

Then you can use GDS:SetAsync(key, string) where the string is a JSONEncode table. This will be good if are only doing it on one server at a time.
And then you can say GDS:GetAsync(key) that will return the saved string.
Also I recommend using tostring(Player.UserId) as part of the key. That will let the data be unique to that player.

As far as getting the dragons and the data you are saving, well that depends on you.

1 Like