My data store is giving someone elses data

Hello dev forums! I’ve been making a lot of progress on my tower defense game but vie come along to find an bug that’s very important to fix, it is that when you teleport into a real game your data is either overwritten or another player in the game gets the towers you own.

I have tested and when the player teleports into the game he automatically gets a random players towers or his own
I have not found any solutions and this is the in game script (not the lobby)

local DSS = game:GetService("DataStoreService")
local MyDataStore = DSS:GetDataStore("PlayerTowers", "Players")

local data = nil

local preset = {
	["Money"] = 0,

	["HotBar"] = {
		["Slot1"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		--["Tower"] = "Mage"},
		["Slot2"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		["Slot3"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		["Slot4"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		["Slot5"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},

	},
	--["Recruit"] = {
	--	["Name"] = "Recruit",
	--	["Bought"] = true,
	--	["Equipped"] = false,
	--	["Slot"] = 0,
	--},

	--["Veteran"] = {
	--	["Name"] = "Veteran",
	--	["Bought"] = false,
	--	["Equipped"] = false,
	--	["Slot"] = 0,
	--},
	["Towers"] = {
          --removed for the dev forums
	}
}
local function GenerateDataKey(Player)
	local Key = "UID_" .. Player.userId
	return Key
end    

game.Players.PlayerAdded:Connect(function(player)

	local key = GenerateDataKey(player)

	local success, errormessage = pcall(function()
		data = MyDataStore:GetAsync(key)
	end)
	

	if data == nil then
		data = preset
		
		
	end
	for i,v in pairs(preset["Towers"]) do
		if data["Towers"][i] ~= nil then
		else
			data["Towers"][i] = preset["Towers"][i]
		end
	end
	game.ReplicatedStorage.GiveTowers:FireClient(player, data)

	for i,v in pairs(data["Towers"]) do
		if v["Bought"] == true then
			local value = Instance.new("ObjectValue",player)
			value.Name = v["Name"]
		end
	end
	
	
	

end)

The “u” should be uppercase
Player.UserId

1 Like

ill see if this works ill message when im done

I think it’s because data is a global variable, so it can overwrite other data variables. I would do this:

local success, data = pcall(function()
		return MyDataStore:GetAsync(key)
end)
if not success then
    -- handle error
end
-- continue as normal
1 Like

userId, is actually valid too.

how would i plug this into my script?
(and how would i handle the error)

local DSS = game:GetService("DataStoreService")
local MyDataStore = DSS:GetDataStore("PlayerTowers", "Players")

local data = nil

local preset = {
	["Money"] = 0,

	["HotBar"] = {
		["Slot1"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		--["Tower"] = "Mage"},
		["Slot2"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		["Slot3"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		["Slot4"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},
		["Slot5"] = {
			["SlotEmpty"] = true, 
			["Tower"] = nil},

	},
	--["Recruit"] = {
	--	["Name"] = "Recruit",
	--	["Bought"] = true,
	--	["Equipped"] = false,
	--	["Slot"] = 0,
	--},

	--["Veteran"] = {
	--	["Name"] = "Veteran",
	--	["Bought"] = false,
	--	["Equipped"] = false,
	--	["Slot"] = 0,
	--},
	["Towers"] = {
          --removed for the dev forums
	}
}
local function GenerateDataKey(Player)
	local Key = "UID_" .. Player.userId
	return Key
end    

game.Players.PlayerAdded:Connect(function(player)

	local key = GenerateDataKey(player)

	local success, data = pcall(function()
		return MyDataStore:GetAsync(key)
    end)
    if not success then
        -- handle error
    end
	

	if data == nil then
		data = preset
		
		
	end
	for i,v in pairs(preset["Towers"]) do
		if data["Towers"][i] ~= nil then
		else
			data["Towers"][i] = preset["Towers"][i]
		end
	end
	game.ReplicatedStorage.GiveTowers:FireClient(player, data)

	for i,v in pairs(data["Towers"]) do
		if v["Bought"] == true then
			local value = Instance.new("ObjectValue",player)
			value.Name = v["Name"]
		end
	end
	
	
	

end)

You could retry upon an error, which is what I would recommend, but you could just use warn.

1 Like

So I imputed what you said but its still not working the handle error i just put a print and it never printed anything (im also not going to reply for a few hours as Im going to sleep)

didnt work any other things I should try?