- What do you want to achieve?
I want to achieve a datspre that actually works and doesn’t get mixed up.
- 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.
- 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!