I get the error on the local attempt = 1 line. I have no issue when running in studio. I only have the issue when running game regularly. Any help is appreciated.
local function LoadData(player)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 3
if success then
print("Data retrieved")
if not playerData then
print("New player, giving default data")
playerData = {
["Points"] = 900,
["SelectedTowers"] = {"Sword Player"},
["OwnedTowers"] = {"Sword Player","Rocket Noob"}
}
end
data[player.UserId] = playerData
else
warn("Unable to get data for player", player.UserId)
player:Kick("There was a problem getting your data")
end
end
Could you post the complete code? It looks like this function may be contained in a larger context given database and data are not defined in your function and the issue may be further up in the script.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local database = DataStoreService:GetDataStore("database")
local functions = ReplicatedStorage:WaitForChild("Functions")
local events = ReplicatedStorage:WaitForChild("Events")
local getDataFunc = functions:WaitForChild("GetData")
local exitEvent = events:WaitForChild("ExitGame")
local data = {}
local function LoadData(player)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 3
if success then
print("Data retrieved")
if not playerData then
print("New player, giving default data")
playerData = {
["Points"] = 900,
["SelectedTowers"] = {"Sword Player"},
["OwnedTowers"] = {"Sword Player","Rocket Noob"}
}
end
data[player.UserId] = playerData
else
warn("Unable to get data for player", player.UserId)
player:Kick("There was a problem getting your data")
end
end
Players.PlayerAdded:Connect(LoadData)
local function SaveData(player)
if data[player.UserId] then
local success = nil
local playerData = nil
local attempt = 1
local info = workspace.Info
local points = math.round(info.Wave.Value / 2)
if info.Message.Value == "VICTORY" then
points = 12
end
data[player.UserId].Points += points
repeat
success, playerData = pcall(function()
return database:UpdateAsync(player.UserId, function()
return data[player.UserId]
end)
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 3
if success then
print("Data saved successfully")
else
warn("Unable to save data for player", player.UserId)
end
else
warn("No session data for", player.UserId)
end
end
exitEvent.OnServerEvent:Connect(function(player)
SaveData(player)
data[player.UserId] = nil
end)
game:BindToClose(function()
if not RunService:IsStudio() then
for index, player in pairs(Players:GetPlayers()) do
task.spawn(function()
SaveData(player)
end)
end
else
print("Shutting down inside studio")
end
end)
getDataFunc.OnServerInvoke = function(player)
return data[player.UserId]
end
There were 2 mentions of the same variable, however, I didn’t see any issue that could be causing this error on these lines, so I just wanted to confirm.