Saving player data and tools attributes upon death

Im making a FPS game that has a perk system. When you select one of these perks (currently, it is used as math.random(1,4) ), each will give you some buffs or nerfs. Here is the script ( Script: ServerScriptService):

local Event = game:GetService("ReplicatedStorage").PowerEvent

Event.OnServerEvent:Connect(function(Player)
	local Value = math.random(1,4)
	
	local AutoBlaster = Player.Backpack:WaitForChild("AutoBlaster")
	local Blaster = Player.Backpack:WaitForChild("Blaster")
	local Raygun = Player.Backpack:WaitForChild("Raygun")

	if Value == 1 then
		Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed +4

		Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth = Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth -25
		Player.Character:FindFirstChildWhichIsA("Humanoid").Health = Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth

		print("+Speed -Heatlh")

	elseif Value == 2 then
		Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed +4

		Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth = Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth -25
		Player.Character:FindFirstChildWhichIsA("Humanoid").Health = Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth

		AutoBlaster:SetAttribute("damage", 6)
		Blaster:SetAttribute("damage", 9)
		Raygun:SetAttribute("damage", 50)

		AutoBlaster:SetAttribute("reloadTime", 1.5)
		Blaster:SetAttribute("reloadTime", 1.2)
		Raygun:SetAttribute("reloadTime", 3.5)

		print("-Damage -Health +Speed -ReloadTime")

	elseif Value == 3 then
		Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth = Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth +25
		Player.Character:FindFirstChildWhichIsA("Humanoid").Health = Player.Character:FindFirstChildWhichIsA("Humanoid").MaxHealth

		AutoBlaster:SetAttribute("damage", 6)
		Blaster:SetAttribute("damage", 9)
		Raygun:SetAttribute("damage", 50)

		print("+Health -Damage")

	elseif Value == 4 then
		Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed -4

		AutoBlaster:SetAttribute("damage", 12)
		Blaster:SetAttribute("damage", 15)
		Raygun:SetAttribute("damage", 80)

		print("+Damage -Speed")
	end
end)

The script works fine. When the player spawns, he is given the perks but upon death loose them completely.

How do I make it so that the player keep his Tools attributes ( “damage”, “reloadTime”), WalkSpeed and MaxHealth and that the data can be changed since the player will have the choice to change perks after each round ?

I’ll try responding as fast as possible !

You could save the data in a folder full of values that are stored somewhere that isn’t destroyed when the player dies.

1 Like

You can store it in a table like this

local Players = game:GetService("Players")
local Event = game:GetService("ReplicatedStorage").PowerEvent

local playerSaves = {}

local function _playerAdded(player)
	player.CharacterAdded:Connect(function(character)
		local saveData = playerSaves[player]
		local humanoid = character and character:WaitForChild("Humanoid")
		local Blaster = player.Backpack:WaitForChild("Blaster")
		local Raygun = player.Backpack:WaitForChild("Raygun")
		local AutoBlaster = player.Backpack:WaitForChild("AutoBlaster")

		if saveData then
			humanoid.WalkSpeed = saveData.walkSpeed
			humanoid.MaxHealth = saveData.maxHealth

			for name, value in playerSaves[player].attributes.autoBlaster do
				AutoBlaster:SetAttribute(name, value)
			end

			for name, value in playerSaves[player].attributes.blaster do
				Blaster:SetAttribute(name, value)
			end

			for name, value in playerSaves[player].attributes.rayGun do
				Raygun:SetAttribute(name, value)
			end
		end
	end)
end

local function _playerRemoving(player)
	playerSaves[player] = nil
end

Event.OnServerEvent:Connect(function(player)
	local Value = math.random(1,4)

	local character = player.Character
	local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")

	local AutoBlaster = player.Backpack:WaitForChild("AutoBlaster")
	local Blaster = player.Backpack:WaitForChild("Blaster")
	local Raygun = player.Backpack:WaitForChild("Raygun")

	if Value == 1 then
		humanoid.WalkSpeed = humanoid.WalkSpeed +4

		humanoid.MaxHealth = humanoid.MaxHealth -25
		humanoid.Health = humanoid.MaxHealth

		print("+Speed -Heatlh")

	elseif Value == 2 then
		humanoid.WalkSpeed = humanoid.WalkSpeed +4

		humanoid.MaxHealth = humanoid.MaxHealth -25
		humanoid.Health = humanoid.MaxHealth

		AutoBlaster:SetAttribute("damage", 6)
		Blaster:SetAttribute("damage", 9)
		Raygun:SetAttribute("damage", 50)

		AutoBlaster:SetAttribute("reloadTime", 1.5)
		Blaster:SetAttribute("reloadTime", 1.2)
		Raygun:SetAttribute("reloadTime", 3.5)

		print("-Damage -Health +Speed -ReloadTime")

	elseif Value == 3 then
		humanoid.MaxHealth = humanoid.MaxHealth +25
		humanoid.Health = humanoid.MaxHealth

		AutoBlaster:SetAttribute("damage", 6)
		Blaster:SetAttribute("damage", 9)
		Raygun:SetAttribute("damage", 50)

		print("+Health -Damage")

	elseif Value == 4 then
		humanoid.WalkSpeed = humanoid.WalkSpeed -4

		AutoBlaster:SetAttribute("damage", 12)
		Blaster:SetAttribute("damage", 15)
		Raygun:SetAttribute("damage", 80)

		print("+Damage -Speed")
	end

	playerSaves[player] = {
		walkSpeed = humanoid.WalkSpeed,
		maxHealth = humanoid.MaxHealth,
		attributes = {
			autoBlaster = {},
			blaster = {},
			rayGun = {},
		}
	}

	for name, value in Blaster:GetAttributes() do
		playerSaves[player].attributes.blaster[name] = value
	end

	for name, value in Raygun:GetAttributes() do
		playerSaves[player].attributes.rayGun[name] = value
	end

	for name, value in AutoBlaster:GetAttributes() do
		playerSaves[player].attributes.autoBlaster[name] = value
	end
end)

for _, player in Players:GetPlayers() do
	_playerAdded(player)
end

Players.PlayerAdded:Connect(_playerAdded)
Players.PlayerRemoving:Connect(_playerRemoving)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.