How can I also save my accessories / tools using DataStore?

Hey!

So I currently have a DataStore script, which works perfectly.
But I also wanted to know how I could save the player tools and accessories.
Is there a way I can do that?

leaderstats Code
-- DataStore Variables

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

----------------------------------------------

-- Attribute Variables

local StarterSpeed = game:GetService("ReplicatedStorage"):WaitForChild("CurrentWalkSpeed").Value
local StarterJumpPower = 50
local starterMaxStamina = 100

local spdPerPoint = 10
local hpPerPoint = 10
local jumpPowerPerPoint = 1
local maxStaminaPerPoint = 10

local speedCap = 80
local jumpCap = 120

local HealthIncrementPerLevel = 5

----------------------------------------------

-- Cancel Quest
local remote = game:GetService("ReplicatedStorage"):WaitForChild("QuestRemote")

remote.OnServerEvent:Connect(function(player, val)
	local questInfoF = player:WaitForChild("QuestInformation")
	if val == "CancelQuest" then
		for i, v in pairs(questInfoF:GetChildren()) do
			if v.className == "StringValue" then
				v.Value = "Default"
			elseif v.className == "IntValue" then
				v.Value = 0
			end
		end
	end
end)

----------------------------------------------------------------------------------------------

game.Players.PlayerAdded:Connect(function(player)
	
	-- Music
	player.PlayerGui:WaitForChild("GameMusic"):Play()

	-----------------
	
	-- Some Variables
	
	local playerID = player.UserId
	
	local character = player.Character
	
	-----------------
		
	-- Folders
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local weapon = Instance.new("Folder", player)
	weapon.Name = "Weapon"
	
	local attacks = Instance.new("Folder", weapon)
	attacks.Name = "WeaponAttacks"

	local projectiles = Instance.new("IntValue", leaderstats)
	projectiles.Name = "Projectiles"
	projectiles.Value = 100

	local currentProjectile = Instance.new("StringValue", leaderstats)
	currentProjectile.Name = "CurrentProjectile"
	currentProjectile.Value = "Shuriken"

	local QuestInformation = Instance.new("Folder", player)
	QuestInformation.Name = "QuestInformation"
	
	local attributes = Instance.new("Folder")
	attributes.Name = "Attributes"
	attributes.Parent = player
	
	local petBoostMultiplier = Instance.new("NumberValue", player)
	petBoostMultiplier.Name = "PetBoostMultiplier"
	petBoostMultiplier.Value = 1

	local hasReadTheTutorial = Instance.new("BoolValue", player)
	hasReadTheTutorial.Name = "HasReadTheTutorial"
	hasReadTheTutorial.Value = false

	local customizedCharacter = Instance.new("BoolValue", player)
	customizedCharacter.Name = "CustomizedCharacter"
	customizedCharacter.Value = false
	
	local loadingScreen = Instance.new("BoolValue", player)
	loadingScreen.Name = "LoadingScreen"
	loadingScreen.Value = false
	
	-----------------
	
	-- leaderstats
	local yen = Instance.new("IntValue", leaderstats)
	yen.Name = "Yen"
	yen.Value = 0
	
	local soulPoints = Instance.new("IntValue", leaderstats)
	soulPoints.Name = "Souls"
	soulPoints.Value = 0

	local Level = Instance.new("IntValue", leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	
	local XP = Instance.new("IntValue", Level)
	XP.Name = "XP"
	XP.Value = 0
	
	local MaxXP = Instance.new("IntValue", Level)
	MaxXP.Name = "MaxXP"
	MaxXP.Value = 100
	
	local rebirths = Instance.new("IntValue", leaderstats)
	rebirths.Name = "Rebirths"
	rebirths.Value = 0
	
	local rebirthTokens = Instance.new("IntValue", leaderstats)
	rebirthTokens.Name = "RebirthTokens"
	rebirthTokens.Value = 0
	
	local rebirthYenBoost = Instance.new("IntValue", leaderstats)
	rebirthYenBoost.Name = "RebirthYenBoost"
	rebirthYenBoost.Value = 1
	
	local stamina = Instance.new("IntValue", leaderstats)
	stamina.Name = "Stamina"
	stamina.Value = 100
	
	local maxStamina = Instance.new("IntValue", stamina)
	maxStamina.Name = "MaxStamina"
	maxStamina.Value = 100
	
	local timePlayed = Instance.new("IntValue", leaderstats)
	timePlayed.Name = "Time Played"
	timePlayed.Value = 0
	
	-----------------

	-- Achievements
	local achievements = Instance.new("Folder", player)
	achievements.Name = "Achievements"
	
	local goldenShurikens = Instance.new("IntValue", achievements)
	goldenShurikens.Name = "GoldenShurikens"
	goldenShurikens.Value = 0
	
	-----------------
	
	-- Weapon
	local currentWeapon = Instance.new("StringValue", weapon)
	currentWeapon.Name = "CurrentWeapon"
	currentWeapon.Value = ""
	
	----------------
	
	-- QuestInfo
	local Title = Instance.new("StringValue", QuestInformation)
	Title.Name = "QuestTitle"
	Title.Value = "Default"

	local Task = Instance.new("StringValue", QuestInformation)
	Task.Name = "QuestTask"
	Task.Value = "Default"

	local Objective = Instance.new("IntValue", QuestInformation)
	Objective.Name = "QuestObjective"
	Objective.Value = 0

	local Progress = Instance.new("IntValue", QuestInformation)
	Progress.Name = "QuestProgress"
	Progress.Value = 0

	local Reward = Instance.new("IntValue", QuestInformation)
	Reward.Name = "QuestReward"
	Reward.Value = 0

	local Reward2 = Instance.new("IntValue", QuestInformation)
	Reward2.Name = "QuestReward2"
	Reward2.Value = 0
	
	-----------------
	
	-- IsUsingAttack Values
	local IsUsingAttack1 = Instance.new("BoolValue", attacks)
	IsUsingAttack1.Name = "IsUsingAttack1"
	IsUsingAttack1.Value = false
	
	local IsUsingAttack2 = Instance.new("BoolValue", attacks)
	IsUsingAttack2.Name = "IsUsingAttack2"
	IsUsingAttack2.Value = false
	
	local IsUsingAttack3 = Instance.new("BoolValue", attacks)
	IsUsingAttack3.Name = "IsUsingAttack3"
	IsUsingAttack3.Value = false
	
	local IsUsingAttack4 = Instance.new("BoolValue", attacks)
	IsUsingAttack4.Name = "IsUsingAttack4"
	IsUsingAttack4.Value = false
	
	local IsUsingAttack5 = Instance.new("BoolValue", attacks)
	IsUsingAttack5.Name = "IsUsingAttack5"
	IsUsingAttack5.Value = false
	
	local IsUsingAttack6 = Instance.new("BoolValue", attacks)
	IsUsingAttack6.Name = "IsUsingAttack6"
	IsUsingAttack6.Value = false

	-----------------
	
	-- Attributes
	local health = Instance.new("IntValue")
	health.Name = "Health"
	health.Parent = attributes
	
	local attack = Instance.new("IntValue")
	attack.Name = "Attack"
	attack.Parent = attributes
	
	local speed = Instance.new("IntValue")
	speed.Name = "Speed"
	speed.Parent = attributes
	
	local maximumStamina = Instance.new("IntValue")
	maximumStamina.Name = "MaximumStamina"
	maximumStamina.Parent = attributes
	
	-----------------
	
	-- Level Up

	XP:GetPropertyChangedSignal('Value'):Connect(function()
		if XP.Value >= MaxXP.Value then
			Level.Value += 1
			XP.Value = 0
			MaxXP.Value *= 1.5
			character.Humanoid.MaxHealth += HealthIncrementPerLevel
		end
	end)
	
	-----------------

	-- Attribute Points

	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local hpRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddHealth")
	local spdRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddSpeed")
	local atkRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddAttack")
	local maxStaminaRemote = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("AddMaxStamina")
	local reset = ReplicatedStorage:WaitForChild("AttributeRemotes"):WaitForChild("Reset")


	local function PointsAvailable(player)
		local PointsUsed = player.Attributes.Health.Value + player.Attributes.Speed.Value + player.Attributes.Attack.Value + player.Attributes.MaxStamina.Value
		local PointsAvailable = player.leaderstats.Level.Value - PointsUsed
		return PointsAvailable > 0
	end

	hpRemote.OnServerEvent:Connect(function(player)
		player.Attributes.Health.Value += 1
		player.Character.Humanoid.MaxHealth = 100 + player.Attributes.Health.Value * hpPerPoint
		player.Character.Humanoid.Health = 100 + player.Attributes.Health.Value * hpPerPoint
	end)

	spdRemote.OnServerEvent:Connect(function(player)
		if player.Attributes.Speed.Value < speedCap then
			player.Attributes.Speed.Value += 1
			player.Character.Humanoid.WalkSpeed = StarterSpeed + player.Attributes.Speed.Value * spdPerPoint
		end
	end)

	atkRemote.OnServerEvent:Connect(function(player)
		player.Attributes.Attack.Value += 1
	end)

	maxStaminaRemote.OnServerEvent:Connect(function(player)
		player.Attributes.MaximumStamina.Value += 1
		player.leaderstats.Stamina.MaxStamina.Value += maxStaminaPerPoint
	end)

	reset.OnServerEvent:Connect(function(player)
		speed.Value = 0
		attack.Value = 0
		health.Value = 0
		maximumStamina.Value = 0
		player.Character.Humanoid.MaxHealth = 100 + player.Attributes.Health.Value * hpPerPoint
		player.Character.Humanoid.Health = 100 + player.Attributes.Health.Value * hpPerPoint
		player.Character.Humanoid.WalkSpeed = StarterSpeed + player.Attributes.Speed.Value * spdPerPoint
		maxStamina.Value = starterMaxStamina
	end)
	-----------------
	
	-- Quest Reward
	local rewarded = false

	Title.Changed:Connect(function()
			Progress.Changed:Connect(function()
			if Progress.Value >= Objective.Value and rewarded == false then
				rewarded = true
				local function GiveReward()
					player:WaitForChild("leaderstats"):WaitForChild("Yen").Value += Reward.Value
					player:WaitForChild("leaderstats"):WaitForChild("Level"):WaitForChild("XP").Value += Reward2.Value
					Title.Value = "Default"
					Task.Value = "Default"
					Progress.Value = 0
					Objective.Value = 0
					Reward.Value = 0
					Reward2.Value = 0
					player:WaitForChild("PlayerGui"):WaitForChild("Notifications"):WaitForChild("CompletedQuest").Visible = true
					wait(2)
					player:WaitForChild("PlayerGui"):WaitForChild("Notifications"):WaitForChild("CompletedQuest").Visible = false
				end
				GiveReward()
				wait(0.001)
				remote:FireClient(player, "CancelQuest")
				wait(0.5)
				rewarded = false
			end
		end)
	end)
	
	-----------------

	-- DataStore

	local data

	local Success, ErrorMessage = pcall(function()
		data = DataStore:GetAsync(playerID)
	end)

	if Success then
		player.leaderstats.Yen.Value = data.Yen -- Saves
		player.leaderstats.Souls.Value = data.Souls -- Saves
		player.leaderstats.Level.Value = data.Level -- Saves
		player.leaderstats.Rebirths.Value = data.Rebirths -- Saves
		player.leaderstats.RebirthTokens.Value = data.RebirthTokens -- Saves
		player.leaderstats.RebirthYenBoost.Value = data.RebirthYenBoost -- Saves
		player.PetBoostMultiplier.Value = data.PetBoostMultiplier -- Saves
		player.leaderstats["Time Played"].Value = data.TimePlayed -- Saves
		
		player.Achievements.GoldenShurikens.Value = data.GoldenShurikens -- Saves
		
		player.Attributes.Health.Value = data.Health -- Saves
		player.Attributes.Speed.Value = data.Speed -- Saves
		player.Attributes.Attack.Value = data.Attack -- Saves
		player.Attributes.MaximumStamina.Value = data.MaximumStamina -- Saves
		
		player.Weapon.CurrentWeapon.Value = data.CurrentWeapon -- Saves
		
		player.leaderstats.Projectiles.Value = data.Projectiles -- Saves
		player.leaderstats.CurrentProjectile.Value = data.CurrentProjectile -- Saves
		
		player.QuestInformation.QuestTitle.Value = data.QuestTitle -- Saves
		player.QuestInformation.QuestTask.Value = data.QuestTask -- Saves
		player.QuestInformation.QuestObjective.Value = data.QuestObjective -- Saves
		player.QuestInformation.QuestProgress.Value = data.QuestProgress -- Saves
		player.QuestInformation.QuestReward.Value = data.QuestReward -- Saves
		player.QuestInformation.QuestReward2.Value = data.QuestReward2 -- Saves
		
		player.CustomizedCharacter.Value = data.CustomizedCharacter
		player.HasReadTheTutorial.Value = data.HasReadTheTutorial
		
		player.leaderstats.Level.MaxXP.Value = data.MaxXP
		player.leaderstats.Level.XP.Value = data.XP
	else
		print("There was an error whilst loading your data. We're sorry!")
		warn(ErrorMessage)
	end
	
	
end)

-------------------------------------------------------------------

-- Save Player's Data

game.Players.PlayerRemoving:Connect(function(player)
	
	local Data = {
		
		Yen = player.leaderstats.Yen.Value, -- Saves
		Souls = player.leaderstats.Souls.Value, -- Saves
		Level = player.leaderstats.Level.Value, -- Saves
		Rebirths = player.leaderstats.Rebirths.Value, -- Saves
		RebirthTokens = player.leaderstats.RebirthTokens.Value, -- Saves
		RebirthYenBoost = player.leaderstats.RebirthYenBoost.Value, -- Saves
		PetBoostMultiplier = player.PetBoostMultiplier.Value, -- Saves
		TimePlayed = player.leaderstats["Time Played"].Value, -- Saves
		
		GoldenShurikens = player.Achievements.GoldenShurikens.Value, -- Saves
		
		Health = player.Attributes.Health.Value, -- Saves
		Speed = player.Attributes.Speed.Value, -- Saves
		Attack = player.Attributes.Attack.Value, -- Saves
		MaximumStamina = player.Attributes.MaximumStamina.Value, -- Saves
		
		CurrentWeapon = player.Weapon.CurrentWeapon.Value, -- Saves
		
		Projectiles = player.leaderstats.Projectiles.Value, -- Saves
		CurrentProjectile = player.leaderstats.CurrentProjectile.Value, -- Saves
		
		QuestTitle = player.QuestInformation.QuestTitle.Value, -- Saves
		QuestTask = player.QuestInformation.QuestTask.Value, -- Saves
		QuestObjective = player.QuestInformation.QuestObjective.Value, -- Saves
		QuestProgress = player.QuestInformation.QuestProgress.Value, -- Saves
		QuestReward = player.QuestInformation.QuestReward.Value, -- Saves
		QuestReward2 = player.QuestInformation.QuestReward2.Value, -- Saves
		
		CustomizedCharacter = player.CustomizedCharacter.Value,
		HasReadTheTutorial = player.HasReadTheTutorial.Value,
		
		MaxXP = player.leaderstats.Level.MaxXP.Value,
		XP = player.leaderstats.Level.XP.Value,
		}
	
	local Success, ErrorMessage = pcall(function()
		DataStore:SetAsync(player.UserId, Data)
	end)
	
	if Success then
		print("Data successfully saved!")
	else
		print("There was an error whilst loading your data. We're sorry!")
		warn(ErrorMessage)
	end
	
end)

------------

-- DataStore in-studio

game:BindToClose(function()
	if game:GetService('RunService'):IsStudio() then
		wait(1)
	end
end)

Thanks, Best Regards,
KracKen

1 Like

You could save the player tools and accesssories by creating (in your case) a intvalue, that has a id of a item. When joining that item id will convert to your accesssor (so like ID1 will become a sword). This is a way but i think there are better.

1 Like

also if this is the ansver would you please mark it as it helped you, so people can see that this forum is already “done”

1 Like

Hmmm sorry that didn’t help. I could do this strategy but how can I code it tho?

Well, making the entire code would take too long (and i am kinda busy), so i’ll explain it in steps, after that you can search it up on the internet how to make every step.

  1. you need to get the accecoires
  2. you need to put the id of the accecoires in a value (just give every accecoire a id)
  3. save the value
  4. when someone joined, if they have this value, you put it in their inventory
1 Like

Is this optimized tho? It looks like it’s going to get tricky

It would be more usefull if you would use tables instead of a lot of intvalues (exploiters can’t see tables, but they can see values). But i didn’t want you to rewrite your code.

1 Like

Make A Script In ServerScriptService And Type this

game.Players.PlayerAdded:Connect(function(Player)
	local CurrentAccessories = Instance.new("Folder" , Player)
	CurrentAccessories.Name = "CurrentAccessories"
	
	--// Wait For Character To Fully Load
	
	wait(10)
	
	--// Now We Will Get The Accessories He Is Currently Using And Save their Names
	
	for _ , v in pairs(Player.Character:GetChildren()) do
		if v:IsA("Accessory") then
			local Name = Instance.new("StringValue" , CurrentAccessories)
			Name.Value = v.Name --// I have seen your "leak video" so I know You Will Use Accessories That Are Saved Somewhere In Your Game, so just loop through them and do whatever you want with them
		end
	end
end
1 Like

I don’t really need this script since I already have a CurrentAccessories folder which stores every clone of the player’s accessories. All I need to know is how to insert them in the DataStore table but I don’t know how.

then do something like this

local DataStoreService = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(Player)
	local CurrentAccessories = game.ReplicatedStorage --Change To The Folder (Not The One Inside The Player But The Main Folder)

	for _ , v in pairs(CurrentAccessories:GetChildren()) do
		local DataStore = DataStoreService:GetDataStore(v.Name)
		local StringValue = Instance.new("StringValue" , Player:WaitForChild("CurrentAccessories"))
		StringValue.Value = DataStore:GetAsync(Player.UserId) or v.Name
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local CurrentAccessories = Player.CurrentAccessories

	for _ , v in pairs(CurrentAccessories:GetChildren()) do
		if v:IsA("StringValue") then
			local DataStore = DataStoreService:GetDataStore(v.Name)
			DataStore:SetAsync(Player.UserId , v.Value)
		end
	end
end)
1 Like