Stuff you need to know before you read below:
I have a data store
ScriptinsideServerScriptService.I have a whole shop GUI (which definitely works)
Every player has 3 folders called
So I have a problem. My script, for some reason, overwrites other players data with mine. I have not done this with some other script, and its definitely not the shop scripts (where you buy the data that is being saved with points which is saved in an entirely different data store script) because it even gave everyone in my server (when I was testing the game with friends) an item which you buy with robux, but no one bought the robux item.
I would also appreciate things that I need to improve in the data store script below, even if it doesn't fix my problem:
~~~~~~~~~~~~~~~~~
-- Variables
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Trails = game.Lighting.Trails
local Tools = game.Lighting.Items
local Tags = game.Lighting.Tags
local ItemsDataStore = DataStoreService:GetDataStore("ItemsDataStore")
local TrailsDataStore = DataStoreService:GetDataStore("TrailsDataStore")
local TagsDataStore = DataStoreService:GetDataStore("TagsDataStore")
local EquippedItemsDataStore = DataStoreService:GetDataStore("EquippedItemsDataStore")
-- Functions
function IsTableEmpty(Table)
local Empty = false
if next(Table) == nil then
Empty = true
end
return Empty
end
-- On player added
game.Players.PlayerAdded:Connect(function(Player)
-- Player variables
local EquippedFolder = Player:WaitForChild("Equipped", 30)
local EquippedTool = EquippedFolder:WaitForChild("Tool", 30)
local EquippedTrail = EquippedFolder:WaitForChild("Trail", 30)
local EquippedTag = EquippedFolder:WaitForChild("Tag", 30)
local PlayerToolsFolder = Player:WaitForChild("PlayerTools", 30)
local PlayerTagsFolder = Player:WaitForChild("PlayerTags", 30)
local PlayerTrailsFolder = Player:WaitForChild("PlayerTrails", 30)
-- Get data (shop items data store)
local su, er = pcall(function()
ToolNamesData = ItemsDataStore:GetAsync(Player.UserId.." -ToolNames")
TrailNamesData = TrailsDataStore:GetAsync(Player.UserId.." -TrailNames")
TagNamesData = TagsDataStore:GetAsync(Player.UserId.." -TagNames")
end)
if su then
if ToolNamesData ~= nil then
PlayerToolNames = HttpService:JSONDecode(ToolNamesData)
end
if TrailNamesData ~= nil then
PlayerTrailNames = HttpService:JSONDecode(TrailNamesData)
end
if TagNamesData ~= nil then
PlayerTagNames = HttpService:JSONDecode(TagNamesData)
end
else
print("There was an error while GETTING "..Player.Name.."'s SHOP items: "..er)
end
-- Get data (equipped items data store)
local su, er = pcall(function()
Data = EquippedItemsDataStore:GetAsync(Player.UserId.." -EquippedItems")
end)
if su then
if Data ~= nil then
PlayerEquippedItemNames = HttpService:JSONDecode(Data)
end
else
print("There was an error while GETTING "..Player.Name.."'s EQUIPPED items: "..er)
end
-- Turn shop items data into real objects
-- Tables
local PlayerTools = {}
local PlayerTags = {}
local PlayerTrails = {}
-- Get tools
if PlayerToolNames ~= nil then
for i, v in pairs(PlayerToolNames) do
if v ~= nil then
local Tool = game.Lighting.Items:FindFirstChild(v)
if Tool then
table.insert(PlayerTools, i, Tool)
end
end
end
end
-- Get tags
if PlayerTagNames ~= nil then
for i, v in pairs(PlayerTagNames) do
local Tag = game.Lighting.Tags:FindFirstChild(v)
if Tag then
table.insert(PlayerTags, i, Tag)
end
end
end
-- Get trails
if PlayerTrailNames ~= nil then
for i, v in pairs(PlayerTrailNames) do
local Trail = game.Lighting.Trails:FindFirstChild(v)
if Trail then
table.insert(PlayerTrails, i, Trail)
end
end
end
-- Put into player
-- Tools
if PlayerTools ~= nil then
for i, v in pairs(PlayerTools) do
local Tool = v:Clone()
Tool.Parent = PlayerToolsFolder
end
end
-- Trails
if PlayerTrails ~= nil then
for i, v in pairs(PlayerTrails) do
local Trail = v:Clone()
Trail.Parent = PlayerTrailsFolder
end
end
-- Tags
if PlayerTags ~= nil then
for i, v in pairs(PlayerTags) do
local Tag = v:Clone()
Tag.Parent = PlayerTagsFolder
end
end
-- Turn equipped items into real objects
if PlayerEquippedItemNames ~= nil then
for i, v in pairs(PlayerEquippedItemNames) do
-- Equip tool
if PlayerToolsFolder:FindFirstChild(v) then
EquippedTool.Value = PlayerToolsFolder:FindFirstChild(v)
end
-- Equip trail
if PlayerTrailsFolder:FindFirstChild(v) then
EquippedTrail.Value = PlayerTrailsFolder:FindFirstChild(v)
end
-- Equip tag
if PlayerTagsFolder:FindFirstChild(v) then
EquippedTag.Value = PlayerToolsFolder:FindFirstChild(v)
end
end
end
end)
-- On player leave
game.Players.PlayerRemoving:Connect(function(Player)
-- Player variables
local EquippedFolder = Player.Equipped
local EquippedTool = EquippedFolder.Tool
local EquippedTrail = EquippedFolder.Trail
local EquippedTag = EquippedFolder.Tag
local PlayerToolsFolder = Player.PlayerTools
local PlayerTagsFolder = Player.PlayerTags
local PlayerTrailsFolder = Player.PlayerTrails
-- Get shop items
-- Variables
local PlayerToolNames = {}
local PlayerTagNames = {}
local PlayerTrailNames = {}
-- Tools
for i, v in pairs(PlayerToolsFolder:GetChildren()) do
table.insert(PlayerToolNames, i, v.Name)
end
-- Tags
for i, v in pairs(PlayerTagsFolder:GetChildren()) do
table.insert(PlayerTagNames, i, v.Name)
end
-- Trails
for i, v in pairs(PlayerTrailsFolder:GetChildren()) do
table.insert(PlayerTrailNames, i, v.Name)
end
-- Check if tables are empty
if IsTableEmpty(PlayerToolNames) == true then
PlayerToolNames = nil
end
if IsTableEmpty(PlayerTagNames) == true then
PlayerTagNames = nil
end
if IsTableEmpty(PlayerTrailNames) == true then
PlayerTrailNames = nil
end
-- Save shop items
-- Tools
if PlayerToolNames ~= nil then
local su, er = pcall(function()
ItemsDataStore:SetAsync(Player.UserId.." -ToolNames", HttpService:JSONEncode(PlayerToolNames))
end)
if su then
else
print("There was an error while SAVING "..Player.Name.."'s tools: "..er)
end
end
-- Tags
if PlayerTagNames ~= nil then
local su, er = pcall(function()
TagsDataStore:SetAsync(Player.UserId.." -TagNames", HttpService:JSONEncode(PlayerTagNames))
end)
if su then
else
print("There was an error while SAVING "..Player.Name.."'s tags: "..er)
end
end
-- Trails
if PlayerTrailNames ~= nil then
local su, er = pcall(function()
TrailsDataStore:SetAsync(Player.UserId.." -TrailNames", HttpService:JSONEncode(PlayerTrailNames))
end)
if su then
else
print("There was an error while SAVING "..Player.Name.."'s trails: "..er)
end
end
-- Get equipped items
-- Table
local EquippedItemsTable = {}
-- Tool
if EquippedTool.Value ~= nil then
table.insert(EquippedItemsTable, 1, EquippedTool.Value.Name)
else
table.insert(EquippedItemsTable, 1, nil)
end
-- Trail
if EquippedTrail.Value ~= nil then
table.insert(EquippedItemsTable, 2, EquippedTrail.Value.Name)
else
table.insert(EquippedItemsTable, 2, nil)
end
-- Tag
if EquippedTag.Value ~= nil then
table.insert(EquippedItemsTable, 3, EquippedTag.Value.Name)
else
table.insert(EquippedItemsTable, 3, nil)
end
-- Save equipped items
local su, er = pcall(function()
EquippedItemsDataStore:SetAsync(Player.UserId.." -EquippedItems", HttpService:JSONEncode(EquippedItemsTable))
end)
if su then
else
print("There was an error while SAVING "..Player.Name.."'s equipped items: "..er)
end
end)
~~~~~~~~~~~~~~~~~
Another thing is that it doesn't print anything when it overwrites someone's data.
By the way, if you don't know what :JSONDecode and :JSONEncode is, it's just making a table into a string in a special weird code that then can be decoded and turned back into a table.
Any help would be appreciated.
