How to save mesh textureId when player leaves game

Hello i am making an FPS game and i want to sell weapon skins. I made a part in game that a player can click to change the mesh textureId of his weapon, the problem is that i need the new mesh textureId to save when the player leaves the game.

Here is the entire script...

script.Parent.MouseClick:connect(function(player)
local gun = player.Backpack:FindFirstChild(“Pistol”)

if player == nil then return end
				
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh
			
mesh.TextureId = "rbxassetid://3910126842" -- Now i need this to be saved to datastore or something. Im not sure what to do next.

end)

The next thing you would need to do is some simple datastore saving using:

Store:SetAysnc()

Store:GetAysnc()

Have you looked into datastores already?

And checking when a player leaves:

https://developer.roblox.com/en-us/api-reference/event/Players/PlayerRemoving

More advanced version:

Another resource:

Yes but i don’t know enough about scripting to understand how to use datastore for this kind of stuff. I only used it for saving player kills and saving tools in a players backpack. I had to use youtube tutorials to do that.

1 Like

Alright, i’ll do it in steps(note the code i am providing is not tested and is to give you an idea, alternatively it would be better to look at some more advanced techniques/datstores (above) at some point )

Firstly we need see when a player leaves the game to save their data(there is no need to save data too often)

----server script in server script service

local players = game:GetService("Players")
local DataStoreService = Game:GetService("DataStoreService")
local TextureIds = DataStoreService:GetDataStore("TextureIds ")-----Fetches a The DataStore we already made


players.PlayerRemoving:Connect(function(plr)---Fires when a player leaves

TextureIds:SetAysnc(plr,  insert the texture id for the player here  )-----the first argument is our player(scope) and the second argument is the string we are saving (texture id)

end)

Now we need to Get the player’s data when they join:

local players = game:GetService("Players")
local DataStoreService = Game:GetService("DataStoreService")
local TextureIds = DataStoreService:GetDataStore("TextureIds ")-----Creates or fetches a datastore 


players.PlayerAdded:Connect(function(plr)---Fires when a player Joins

TextureId =  TextureIds:GetAysnc(plr)---The only argument we need is the player 

end)

Now Implementing all of the code above into your code would look something like this:

---local script
script.Parent.MouseClick:connect(function(player)
local gun = player.Backpack:FindFirstChild(“Pistol”)



if player == nil then return end
				
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh
			
mesh.TextureId = "rbxassetid://3910126842" -- Now i need this to be saved to datastore or something. Im not sure what to do next.


end)
---Server Script

local players = game:GetService("Players")
local DataStoreService = Game:GetService("DataStoreService")
local TextureIds = DataStoreService:GetDataStore("TextureIds ")-----Creates or fetches a datastore 


players.PlayerAdded:Connect(function(plr)---Fires when a player leaves

local gun = plr.Backpack:FindFirstChild(“Pistol”)
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh

mesh.TextureId =  TextureIds:GetAysnc(plr)---The only argument we need is the player, this sets the mesh.TextureId to the saved value 

end)


players.PlayerRemoving:Connect(function(plr)---Fires when a player leaves 

local gun = plr.Backpack:FindFirstChild(“Pistol”)
local handle = gun:FindFirstChild("Handle")
local mesh = handle.Mesh

 TextureIds:SetAysnc(plr, mesh.TextureId ) ---Saving the data!

end)


Hopefully that was somewhat helpful!
if you have any questions let me know :grinning:
(Also i strongly advise you take a look a those resources they’re very helpful)

2 Likes