Help with script

Can someone with a good heart help me with a script, how do I transform this script into a table, as it is exeding the datastore requests

Script:

	--PVP System
	Bounty.Value = Bounty1:GetAsync(Plr.UserId) or Bounty.Value
	Bounty1:SetAsync(Plr.UserId, Bounty.Value)
	Bounty.Changed:connect(function()
		Bounty1:SetAsync(Plr.UserId, Bounty.Value)
	end)
	--PVP System
	PvpSystem.Value = PvpSystem1:GetAsync(Plr.UserId) or PvpSystem.Value
	PvpSystem1:SetAsync(Plr.UserId, PvpSystem.Value)
	PvpSystem.Changed:connect(function()
		PvpSystem1:SetAsync(Plr.UserId, PvpSystem.Value)
	end)
	--- Levels
   Levels.Value = Level1:GetAsync(Plr.UserId) or Levels.Value
	   Level1:SetAsync(Plr.UserId, Levels.Value)
	Levels.Changed:connect(function()
	   Level1:SetAsync(Plr.UserId, Levels.Value)
   end)
	--- Gold
   Beli.Value = Beli11:GetAsync(Plr.UserId) or Beli.Value
	   Beli11:SetAsync(Plr.UserId, Beli.Value)
	Beli.Changed:connect(function()
	   Beli11:SetAsync(Plr.UserId, Beli.Value)
	end)
	-- Gems
	Gems.Value = Gems1:GetAsync(Plr.UserId) or Gems.Value
	Gems1:SetAsync(Plr.UserId, Gems.Value)
	Gems.Changed:connect(function()
		Gems1:SetAsync(Plr.UserId, Gems.Value)
	end)
	--- Exp
   Exp.Value = Exp1:GetAsync(Plr.UserId) or Exp.Value
	   Exp1:SetAsync(Plr.UserId, Exp.Value)
	Exp.Changed:connect(function()
	   Exp1:SetAsync(Plr.UserId, Exp.Value)
   end)
	--- ExpNeed
   ExpNeed.Value = ExpNeed1:GetAsync(Plr.UserId) or ExpNeed.Value
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
	ExpNeed.Changed :connect(function()
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
	end)
	--- HealthAdd
	HealthAdd.Value = HealthAdd1:GetAsync(Plr.UserId) or HealthAdd.Value
	HealthAdd1:SetAsync(Plr.UserId, HealthAdd.Value)
	HealthAdd.Changed:connect(function()
		HealthAdd1:SetAsync(Plr.UserId, HealthAdd.Value)
	end)
	--- SwordP
   SwordP.Value = SwordP1:GetAsync(Plr.UserId) or SwordP.Value
	   SwordP1:SetAsync(Plr.UserId, SwordP.Value)
	SwordP.Changed:connect(function()
	   SwordP1:SetAsync(Plr.UserId, SwordP.Value)
   end)
	--- DefenseP
   DefenseP.Value = DefenseP1:GetAsync(Plr.UserId) or DefenseP.Value
	   DefenseP1:SetAsync(Plr.UserId, DefenseP.Value)
	DefenseP.Changed:connect(function()
	   DefenseP1:SetAsync(Plr.UserId, DefenseP.Value)
   end)
	--- Sword
   Sword.Value = Sword1:GetAsync(Plr.UserId) or Sword.Value
	   Sword1:SetAsync(Plr.UserId, SwordP.Value)
	Sword.Changed:connect(function()
	   Sword1:SetAsync(Plr.UserId, SwordP.Value)
   end)
	--- Defense
   Defense.Value = Defense1:GetAsync(Plr.UserId) or Defense.Value
	   Defense1:SetAsync(Plr.UserId, Defense.Value)
	Defense.Changed:connect(function()
	   Defense1:SetAsync(Plr.UserId, Defense.Value)
   end)
	--- Luck
   Luck.Value = Luck1:GetAsync(Plr.UserId) or Luck.Value
	   Luck1:SetAsync(Plr.UserId, Luck.Value)
	Luck.Changed:connect(function()
	   Luck1:SetAsync(Plr.UserId, Luck.Value)
   end)
	--- Special
   Special.Value = Special1:GetAsync(Plr.UserId) or Special.Value
	   Special1:SetAsync(Plr.UserId, Special.Value)
	Special.Changed:connect(function()
	   Special1:SetAsync(Plr.UserId, Special.Value)
	end)
	--- Points
   Points.Value = Points1:GetAsync(Plr.UserId) or Points.Value
	   Points1:SetAsync(Plr.UserId, Points.Value)
		Points.Changed:connect(function()
	   Points1:SetAsync(Plr.UserId, Points.Value)
   end)

You could have something like this -

local default_data = {
Bounty = 0,
PvpSystem = 0,
Levels = 1,
Beli = 0,
Gems = 0,
Exp = 0,
ExpNeed = 100,
HealthAdd = 0,
SwordP = 0,
DefenseP = 0,
Sword = "Basic Sword",
Defense = 0,
Luck = 0,
Special = 0,
Points = 0,
}

you will need to do some changes to the values and load it/save it in your data

1 Like

Here is an example:

local DataStoreService = game:GetService("DataStoreService")
local CornerData: DataStore = DataStoreService:GetDataStore("46W")

local player_default_data = {
	Level = 1,
	XP = 0,
	Gold = 0,
	MaxHp = 100,
	Speed = 16,
	Class = "Warrior",
	Resist = 0,
	SwordExtra = 0,
	AxeExtra = 0,
	SpellsExtra = 0,
	Armor = "",
	Helmet = "",
	Mana = 40,
	CurrentMana = 40,
}

local function load(key)
	local received, result = pcall(function()
		local dataReceived = CornerData:GetAsync(key)
		return dataReceived
	end)

	if received then
		print('Loaded')
		return result or player_default_data  
	else
		warn(result) 
	end
end

local function save(key, data)

	local success, errorMessage = pcall(function()
		CornerData:SetAsync(key, data)
	end)

	if success then
		print('Saved')
	else
		warn(errorMessage)
	end

end

game.Players.PlayerAdded:Connect(function(player)

	local dataIsLoading = Instance.new('BoolValue', player) 
	dataIsLoading.Name = 'DataIsLoading'

	local playerData = load(player.UserId)  

	if playerData then
		player.CharacterAdded:Connect(function(char)
			char.Humanoid.WalkSpeed = player:WaitForChild("leaderstats").Speed.Value
			char.Humanoid.MaxHealth = player:WaitForChild("leaderstats").MaxHp.Value
			char.Humanoid.Health = player:WaitForChild("leaderstats").MaxHp.Value
		end)
		
		dataIsLoading:Destroy()  

		local infoFolder = Instance.new('Folder', player)
		infoFolder.Name = "leaderstats"

		local level = Instance.new('IntValue', infoFolder)
		level.Name = 'Level'
		level.Value = playerData.Level

		local xp = Instance.new('IntValue', infoFolder)
		xp.Name = 'XP'
		xp.Value = playerData.XP

		local gold = Instance.new('IntValue', infoFolder)
		gold.Name = 'Gold'
		gold.Value = playerData.Gold


		local Beginnerclass = Instance.new('StringValue', infoFolder)
		Beginnerclass.Name = 'Class'
		Beginnerclass.Value = playerData.Class

		local HP = Instance.new('IntValue', infoFolder)
		HP.Name = 'MaxHp'
		HP.Value = playerData.MaxHp

		local SPEED = Instance.new('IntValue', infoFolder)
		SPEED.Name = 'Speed'
		SPEED.Value = playerData.Speed
		
		local MANA = Instance.new('IntValue', infoFolder)
		MANA.Name = 'Mana'
		MANA.Value = playerData.Mana
		
		local CurrentMAana = Instance.new('IntValue', infoFolder)
		CurrentMAana.Name = 'CurrentMana'
		CurrentMAana.Value = playerData.CurrentMana

		local RES = Instance.new('NumberValue', infoFolder)
		RES.Name = 'Resist'
		RES.Value = playerData.Resist

		local ad = Instance.new('NumberValue', infoFolder)
		ad.Name = 'AxeExtra'
		ad.Value = playerData.AxeExtra


		local ab = Instance.new('NumberValue', infoFolder)
		ab.Name = 'SwordExtra'
		ab.Value = playerData.SwordExtra

		local st = Instance.new('NumberValue', infoFolder)
		st.Name = 'SpellsExtra'
		st.Value = playerData.SpellsExtra

		local ARmor = Instance.new('StringValue', infoFolder)
		ARmor.Name = 'Armor'
		ARmor.Value = playerData.Armor

		local HElmet = Instance.new('StringValue', infoFolder)
		HElmet.Name = 'Helmet'
		HElmet.Value = playerData.Helmet



	else
		warn('DATA FAILED TO LOAD')
		player:Kick('Your data failed to load, please rejoin.') 
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local infoFolder = player:FindFirstChild("leaderstats")
	local playerDataLoaded = player:FindFirstChild('DataIsLoading') == nil  

	if infoFolder and playerDataLoaded then
		player_data = {
			Level = infoFolder.Level.Value,
			XP = infoFolder.XP.Value,
			Gold = infoFolder.Gold.Value,
			MaxHp = infoFolder.MaxHp.Value,
			Speed = infoFolder.Speed.Value,
			Class = infoFolder.Class.Value,
			Resist = infoFolder.Resist.Value,
			SwordExtra =infoFolder.SwordExtra.Value,
			AxeExtra = infoFolder.AxeExtra.Value,
			SpellsExtra = infoFolder.SpellsExtra.Value,
			Armor = infoFolder.Armor.Value,
			Helmet = infoFolder.Helmet.Value,
			Mana = infoFolder.Mana.Value,
			CurrentMana = infoFolder.CurrentMana.Value,
		}

		save(player.UserId, player_data) 
	end
end)



game:BindToClose(function()
	for _,Player in pairs(game.Players:GetPlayers()) do
		task.wait(#game.Players:GetPlayers() > 1 and #game.Players:GetPlayers()/2 or 1)
		local infoFolder = Player:FindFirstChild("leaderstats")
		player_data = {
			Level = infoFolder.Level.Value,
			XP = infoFolder.XP.Value,
			Gold = infoFolder.Gold.Value,
			MaxHp = infoFolder.MaxHp.Value,
			Speed = infoFolder.Speed.Value,
			Class = infoFolder.Class.Value,
			Resist = infoFolder.Resist.Value,
			SwordExtra =infoFolder.SwordExtra.Value,
			AxeExtra = infoFolder.AxeExtra.Value,
			SpellsExtra = infoFolder.SpellsExtra.Value,
			Armor = infoFolder.Armor.Value,
			Helmet = infoFolder.Helmet.Value,
			Mana = infoFolder.Mana.Value,
			CurrentMana = infoFolder.CurrentMana.Value,
		}
		local success, err  = pcall(function()
			save(Player.UserId,player_data) 
		end)
	end
end)
2 Likes

More like, it’s because when I killed one enemy after another it exceeded as requests so I need to make a table to get a value when it changes.

Example: I won 50 xp, then the script will pick up what was changed and run SetAsync

Why do you have to do it every time the player gets XP? Why not just update the datastore when the player leaves?

4 Likes

I agree with this. The user should really save it when the player leaves and if they really want ever like 5-10 minutes if they want to ensure everything is saved.

Still should have it in a table really though due to how much data is being saved here.

1 Like

Well, I did the table/loading and saving as I_oL
said above, so any value I change, be it exp, or anything else, when I exit will be saved without any problem?

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

local player_default_data = {
	Class = "None",
	Bounty = 0,
	Level = 1,
	Gold = 0,
	Gems = 0,
	Exp = 0,
	ExpNeed = 100,
	HealthAdd = 0,
	DefenseP = 0,
	SwordP = 0,
	LuckP = 0,
	SpecialP = 0,
	PointsS = 1,

	Defense = 0,
	Sword = 0,
	Luck = 0,
	Special = 0,
	Points = 3,
	PvpSystem = 0,
}

local function load(key)
	local received, result = pcall(function()
		local dataReceived = CornerData:GetAsync(key)
		return dataReceived
	end)

	if received then
		print('Loaded')
		return result or player_default_data  
	else
		warn(result) 
	end
end

local function save(key, data)

	local success, errorMessage = pcall(function()
		CornerData:SetAsync(key, data)
	end)

	if success then
		print('Saved')
	else
		warn(errorMessage)
	end

end

game.Players.PlayerAdded:Connect(function(player)

	local dataIsLoading = Instance.new('BoolValue', player) 
	dataIsLoading.Name = 'DataIsLoading'

	local playerData = load(player.UserId)  

	if playerData then
		player.CharacterAdded:Connect(function(char)
			char.Humanoid.MaxHealth = 100 + player:WaitForChild("Data").HealthAdd.Value
			char.Humanoid.Health = char.Humanoid.MaxHealth
		end)

		dataIsLoading:Destroy()  

		-- Folders
		local infoFolder = Instance.new('Folder', player)
		infoFolder.Name = "Data"

		local leaderFolder = Instance.new('Folder', player)
		leaderFolder.Name = "leaderstats"

		-- Databases
		local bounty = Instance.new('IntValue', leaderFolder)
		bounty.Name = 'Level'
		bounty.Value = playerData.Bounty

		local level = Instance.new('IntValue', infoFolder)
		level.Name = 'Levels'
		level.Value = playerData.Level

		local exp = Instance.new('IntValue', infoFolder)
		exp.Name = 'Exp'
		exp.Value = playerData.Exp

		local expneed = Instance.new('IntValue', infoFolder)
		expneed.Name = 'ExpNeed'
		expneed.Value = playerData.ExpNeed

		local gold = Instance.new('IntValue', infoFolder)
		gold.Name = 'Gold'
		gold.Value = playerData.Gold

		local gems = Instance.new('IntValue', infoFolder)
		gold.Name = 'Gems'
		gold.Value = playerData.Gems

		local Beginnerclass = Instance.new('StringValue', infoFolder)
		Beginnerclass.Name = 'Class'
		Beginnerclass.Value = playerData.Class

		local healthadd = Instance.new('IntValue', infoFolder)
		healthadd.Name = 'HealthAdd'
		healthadd.Value = playerData.HealthAdd

		local defensep = Instance.new('IntValue', infoFolder)
		defensep.Name = 'DefenseP'
		defensep.Value = playerData.DefenseP

		local swordp = Instance.new('IntValue', infoFolder)
		swordp.Name = 'SwordP'
		swordp.Value = playerData.SwordP

		local luckp = Instance.new('IntValue', infoFolder)
		luckp.Name = 'LuckP'
		luckp.Value = playerData.LuckP

		local specialp = Instance.new('IntValue', infoFolder)
		specialp.Name = 'SpecialP'
		specialp.Value = playerData.SpecialP

		local pointss = Instance.new('IntValue', infoFolder)
		pointss.Name = 'PointsS'
		pointss.Value = playerData.PointsS

		-- Stats
		local defense = Instance.new('IntValue', infoFolder)
		defense.Name = 'Defense'
		defense.Value = playerData.Defense

		local sword = Instance.new('IntValue', infoFolder)
		sword.Name = 'Sword'
		sword.Value = playerData.Sword

		local luck = Instance.new('IntValue', infoFolder)
		luck.Name = 'Luck'
		luck.Value = playerData.Luck

		local special = Instance.new('IntValue', infoFolder)
		special.Name = 'Special'
		special.Value = playerData.Special

		local points = Instance.new('IntValue', infoFolder)
		points.Name = 'Points'
		points.Value = playerData.Points

		local pvpsystem = Instance.new('IntValue', infoFolder)
		pvpsystem.Name = 'PvpSystem'
		pvpsystem.Value = playerData.PvpSystem


	else
		warn('DATA FAILED TO LOAD')
		player:Kick('Your data failed to load, please rejoin.') 
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local infoFolder = player:FindFirstChild("leaderstats")
	local playerDataLoaded = player:FindFirstChild('DataIsLoading') == nil  

	if infoFolder and playerDataLoaded then
		player_data = {
			Class = infoFolder.Class.Value,
			Bounty = infoFolder.Bounty.Value,
			Level = infoFolder.Level.Value,
			Gold = infoFolder.Gold.Value,
			Gems = infoFolder.Gems.Value,
			Exp = infoFolder.Exp.Value,
			ExpNeed = infoFolder.ExpNeed.Value,
			HealthAdd = infoFolder.HealthAdd.Value,
			DefenseP = infoFolder.DefenseP.Value,
			SwordP = infoFolder.SwordP.Value,
			LuckP = infoFolder.LuckP.Value,
			SpecialP = infoFolder.SpecialP.Value,
			PointsS = infoFolder.PointsS.Value,
			Defense = infoFolder.Defense.Value,
			Sword = infoFolder.Sword.Value,
			Luck = infoFolder.Luck.Value,
			Special = infoFolder.Special.Value,
			Points = infoFolder.Points.Value,
			PvpSystem = 0,
		}

		save(player.UserId, player_data) 
	end
end)



game:BindToClose(function()
	for _,Player in pairs(game.Players:GetPlayers()) do
		task.wait(#game.Players:GetPlayers() > 1 and #game.Players:GetPlayers()/2 or 1)
		local infoFolder = Player:FindFirstChild("leaderstats")
		player_data = {
			Class = infoFolder.Class.Value,
			Bounty = infoFolder.Bounty.Value,
			Level = infoFolder.Level.Value,
			Gold = infoFolder.Gold.Value,
			Gems = infoFolder.Gems.Value,
			Exp = infoFolder.Exp.Value,
			ExpNeed = infoFolder.ExpNeed.Value,
			HealthAdd = infoFolder.HealthAdd.Value,
			DefenseP = infoFolder.DefenseP.Value,
			SwordP = infoFolder.SwordP.Value,
			LuckP = infoFolder.LuckP.Value,
			SpecialP = infoFolder.SpecialP.Value,
			PointsS = infoFolder.PointsS.Value,
			Defense = infoFolder.Defense.Value,
			Sword = infoFolder.Sword.Value,
			Luck = infoFolder.Luck.Value,
			Special = infoFolder.Special.Value,
			Points = infoFolder.Points.Value,
			PvpSystem = 0,
		}
		local success, err  = pcall(function()
			save(Player.UserId,player_data) 
		end)
	end
end)

how do i put a level up system in this script?

When a person gains XP check if XP > LvlUpAmount, if it’s greater then increase their level by one. You could do that by just getting the table of values you will save when they leave doing:

Table["Level"] += 1

It would probably be easiest if this was done using a server side module script.

1 Like