Issue with data getting mixed up with other players data!

  1. What do you want to achieve?

I want to achieve a datspre that actually works and doesn’t get mixed up.

  1. What is the issue?

The issue is when a players buys boxing gloves and then leaves and join back if another player is in the game then it would give them the boxing glove that the player who left had for someone reason. I think this happens when players join at the same time or if a player leaves I am not to sure.

  1. What solutions have you tried so far?

Finding a solution

Player handler script.

local DatastoreSystems = {
    require(script.Inventory) -- Inventory System Module
}
local CloseEvent = Instance.new("BindableEvent")
local DatastoreService = game:GetService("DataStoreService")
local Datastore = nil
local DatastoreConfig = {
    Key = "UserId",
    Name = "YourDatasRodexdreNdsfamdsfeeggddtdOOF^17"
}
-- Old Name: YouDataStoreName

function init()
    Datastore = DatastoreService:GetDataStore(DatastoreConfig.Name)
    local success , err = pcall(function()
        return Datastore:GetAsync("-TestConnection")
    end)
    if not success then
        Datastore = nil
        error("ERROR: With Datastore,: "..err)
    else
        Datastore = DatastoreService:GetDataStore(DatastoreConfig.Name)
    end
end

game.Players.PlayerAdded:Connect(function(player)
    local PlayerData = Instance.new("Folder")
    PlayerData.Parent = game.ServerStorage.PlayerData
    PlayerData.Name = player.Name
    
    for i,v in pairs(DatastoreSystems) do
        v:new(player,PlayerData,DatastoreConfig.Key)
        v:SetValue(Datastore)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    for i,v in pairs(DatastoreSystems) do
        v:Save(Datastore)
    end
    
    game.ServerStorage.PlayerData:FindFirstChild(player.Name):Destroy()
end)

game:BindToClose(function()
    wait(2)
end)

init()

Inventory module script

local Inventory = {}
local InventoryKey = "-Inventory"

function Inventory:new(Player,Folder,Key) 
	local InventoryFolder = Instance.new("Folder", Folder)
	InventoryFolder.Name = "Inventory"
	
	local Gloves = Instance.new("Folder", InventoryFolder)
	Gloves.Name = "Gloves"
	
	local Equipped = Instance.new("Folder", InventoryFolder)
	Equipped.Name = "Equipped"
	
	local EquippedGloves = Instance.new("Folder", Equipped)
	EquippedGloves.Name = "Gloves"
	
	self.Player = Player
	self.Key = Key
	self.InventoryFolder = InventoryFolder
end

function Inventory:SetValue(Datastore) 
	local success , result = pcall(function()
		return Datastore:GetAsync(self.Player[self.Key]..InventoryKey)
	end)
	
	if success then
		
		if result ~= nil and result.Gloves ~= nil then --For Gloves
			for _,v in pairs(result.Gloves) do
				if game.ServerStorage.Shop.Gloves:FindFirstChild(v) then
					local Glove = game.ServerStorage.Shop.Gloves:FindFirstChild(v):Clone()
					Glove.Parent = game.ServerStorage.PlayerData[self.Player.Name].Inventory.Gloves
					
					if Glove:FindFirstChild("ToolProperties") then
						local ConfigModule = require(Glove:FindFirstChild("ToolProperties"))
						
						--[[for a,toolScript in pairs(ConfigModule.Scripts) do
							toolScript.Disabled = false
						end]]
						
						Glove:FindFirstChild("ToolProperties"):Destroy()
					else
						error("Configuration Module Not Found For Tool: "..Glove.Name)
					end
				end
			end
		end
		
		if result ~= nil and result.Equipped ~= nil then --For Equipped
			if result.Equipped.Gloves then
				for _,v in pairs(result.Equipped.Gloves) do -- Gloves
					if game.ServerStorage.PlayerData[self.Player.Name].Inventory.Gloves:FindFirstChild(v) then
						local Glove = game.ServerStorage.Shop.Gloves:FindFirstChild(v):Clone()
						Glove.Parent = game.ServerStorage.PlayerData[self.Player.Name].Inventory.Equipped.Gloves

						local GloveStarterGear = game.ServerStorage.Shop.Gloves:FindFirstChild(v):Clone()
						GloveStarterGear.Parent = self.Player.StarterGear
						
						local GloveBackpack = game.ServerStorage.Shop.Gloves:FindFirstChild(v):Clone()
						GloveBackpack.Parent = self.Player.Backpack

						if GloveBackpack:FindFirstChild("ToolProperties") then
							local ConfigModule = require(GloveBackpack:FindFirstChild("ToolProperties"))
							
							for a,toolScript in pairs(ConfigModule.Scripts) do
								toolScript.Disabled = false
							end

							Glove:FindFirstChild("ToolProperties"):Destroy()
							GloveBackpack:FindFirstChild("ToolProperties"):Destroy()
						else
							error("Configuration Module Not Found For Tool: "..Glove.Name)
						end
						
						if GloveStarterGear:FindFirstChild("ToolProperties") then
							local ConfigModule = require(GloveStarterGear:FindFirstChild("ToolProperties"))

							for a,toolScript in pairs(ConfigModule.Scripts) do
								toolScript.Disabled = false
							end

							GloveStarterGear:FindFirstChild("ToolProperties"):Destroy()
						else
							error("Configuration Module Not Found For Tool: "..Glove.Name)
						end
					end
				end
			end
		end
	else
		error(result)
	end
end

function Inventory:Save(Datastore)
	
	local success , result = pcall(function()
		local InventorySave = {}
		InventorySave["Equipped"] = {
			Gloves = {}
		}
		InventorySave["Gloves"] = {}
		for i,v in pairs(game.ServerStorage.PlayerData[self.Player.Name].Inventory:GetChildren()) do
			for a,b in pairs(v:GetChildren()) do
				if v.Name == "Equipped" then
					for c,d in pairs(b:GetChildren()) do
						InventorySave.Equipped.Gloves[#InventorySave.Equipped.Gloves + 1] = d.Name
					end
				else
					InventorySave[v.Name][#InventorySave[v.Name] + 1] = b.Name
				end
			end
		end
		
		return Datastore:SetAsync(self.Player[self.Key]..InventoryKey, InventorySave)
	end)
	
	if success then
		print("Data Saved")
	else
		error(result)
	end
end

return Inventory

Thank you in advance!

This makes it very annoying for players playing game as there gloves sometimes dissappear out of there inventory.

You should be using a unique DataStore key for each player (typically scripts make use of the player’s UserId as that is unique to them and cannot be changed).

How would I do that I thought this script already has all of that, I got someone to make this script btw.

You’re using a custom datastore module script so I wouldn’t know its documentation, what is it you’re trying to save? It might be best to just create a new script using Roblox’s vanilla DataStore service.

I am basically trying to save what ever the player buys and what gloves they equip

Do you think it could be to do with this

game:BindToClose(function()
    wait(2)
end)

Still haven’t found a solution and is game breaking because players are losing there data!

This is a very difficult way of making a datastore. All you have to do is setasync the player.userid, and a table. Inside the table are values. When the player joins again it will get the data through the key (player.userid) and then can get the table values.
You don’t need bindableevents for this.

I still don’t really know what you mean, doesn’t the script already do all those thing?

Hello! Sorry for late response. I feel bad for not adding onto your reply, so if you still need help check out a tutorial I made. How to use Datastore-Service correctly and fix dataloss
:slight_smile: