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)
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)