I have a script that is supposed to save what the player has equipped. When a player equips a tool, it works. It is a GUI button that connects to a remote event. There are no errors, but however it does not save the players last equipped primary and secondary. API services are enabled in the place. This is the script:

local new = Instance.new

local datastore = game:GetService("DataStoreService"):GetDataStore("LocalPlayerPrimary")
local updateAsync = datastore.UpdateAsync
local datastore2 = game:GetService("DataStoreService"):GetDataStore("LocalPlayerSecondary")
local updateAsync2 = datastore2.UpdateAsync

local loadData1 = function(player)
	local id = player.UserId
	local data

	local success,output = pcall(function()
		data = datastore:GetAsync(id) 
	end)

	assert(success,output)

	if not data then
		data = {
			PrimaryEquipped = "Laser Gun",
		}
	end	


	player.PrimaryEquipped.Value = data.PrimaryEquipped
end

local loadData2 = function(player)
	local id = player.UserId
	local data

	local success,output = pcall(function()
		data = datastore2:GetAsync(id) 
	end)

	assert(success,output)

	if not data then
		data = {
			SecondaryEquipped = "Speed Coil",
		}
	end	


	player.SecondaryEquipped.Value = data.SecondaryEquipped
end

local saveData1 = function(player)
	local id = player.UserId
	local data = {
		PrimaryEquipped = "",
	}



	data.PrimaryEquipped = player.PrimaryEquipped.Value

	local success,output = pcall(updateAsync,datastore,id,function(old)
		return data
	end)

	assert(success,output)
end

local saveData2 = function(player)
	local id = player.UserId
	local data = {
		SecondaryEquipped = "",
	}



	data.SecondaryEquipped = player.SecondaryEquipped.Value

	local success,output = pcall(updateAsync2,datastore2,id,function(old)
		return data
	end)

	assert(success,output)
end

updatePri = function(player)
	
	local clone = game.ReplicatedStorage.Items:FindFirstChild(player.PrimaryEquipped.Value)
	local tool = clone:Clone()
	tool.Parent = player:WaitForChild("Backpack")


	
end

updateSec = function(player)

	local clone = game.ReplicatedStorage.Items:FindFirstChild(player.SecondaryEquipped.Value)
	local tool = clone:Clone()
	tool.Parent = player:WaitForChild("Backpack")



end

local onJoin = function(player)

	-- // Equipped Character Value
	local p = new("StringValue")
	p.Parent = player
	p.Name = "PrimaryEquipped"

	local s = new("StringValue")
	s.Parent = player
	s.Name = "SecondaryEquipped"
	
	loadData1(player)
	loadData2(player)

end



game.Players.PlayerAdded:Connect(onJoin)
game.Players.PlayerRemoving:Connect(saveData1, saveData2)


Can anyone help? Thank you.

1 comment

0

UPDATE: The first value saves, the second one doesnt.