So i have a script that automatically erases data when the reservedserver closes but for some reason it doesnt work
Here is my code.
local DataStoreService = game:GetService("DataStoreService")
local Servers = DataStoreService:GetDataStore("Servers")
local Key = "ServersActive"
local Key2 = "ServersLocation"
local AccessCode = nil
local GetTable = nil
local Success, Error = pcall(function()
GetTable = Servers:GetAsync(Key)
end)
if Success then
AccessCode = GetTable[#GetTable]
end
local HttpService = game:GetService("HttpService")
local API = "http://ip-api.com/json/"
local Info = nil
local S, E = pcall(function()
Info = HttpService:JSONDecode(HttpService:GetAsync(API))
end)
local Location = nil
if Success then
Location = tostring(Info["city"])..", "..tostring(Info["country"])
end
game.ReplicatedStorage.ServerLocation.Value = Location
local GetTable3 = nil
local Success, Error = pcall(function()
GetTable3 = Servers:GetAsync(Key2)
end)
if Success then
table.insert(GetTable3, {Location, game.JobId})
local Success, Error = pcall(function()
Servers:SetAsync(Key2, GetTable3)
end)
end
--wont erase data
game:BindToClose(function()
local GetTable2 = nil
local Success, Error = pcall(function()
GetTable2 = Servers:GetAsync(Key)
end)
if Success then
table.remove(GetTable2, table.find(GetTable2, AccessCode))
local Success3, Error3 = pcall(function()
Servers:SetAsync(Key, GetTable2)
end)
local GetTable4 = nil
local Success2, Error2 = pcall(function()
GetTable4 = Servers:GetAsync(Key2)
end)
if Success2 then
table.remove(GetTable4, table.find(GetTable4[2], game.JobId))
local Success4, Error4 = pcall(function()
Servers:SetAsync(Key2, GetTable4)
end)
end
end
end)