Hello, I’m using a RemoteFunction so the client can request it’s data. I’m pretty sure it’s because it is not getting the “player” variable, but I don’t understand why.
Client, narrowed down to the part that is having an issue.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local eventsFolder = ReplicatedStorage:WaitForChild("Events")
local requestNightDataEvent = eventsFolder:WaitForChild("RequestNightData")
local localPlayer = game.Players.LocalPlayer
local maxUnlockedNight = requestNightDataEvent:InvokeServer("MaxNightUnlocked")
Server
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local database = DataStoreService:GetDataStore("data")
local sessionData = {}
local eventsFolder = ReplicatedStorage:WaitForChild("Events")
local requestNightDataEvent = eventsFolder:WaitForChild("RequestNightData")
function PlayerAdded(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(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default player data")
playerData = {
["MaxNightUnlocked"] = 1,
["Wins"] = 0
}
end
sessionData[player.UserId] = playerData
else
warn("Unable to get data for", player.UserId)
player:Kick("Unable to load your data. If this problem persists try again at a later time.")
end
-- add leaderstats here
end
Players.PlayerAdded:Connect(PlayerAdded)
function PlayerRemoving(player)
if sessionData[player.UserId] then
local success = nil
local errorMessage = nil
local attempt = 1
repeat
success, errorMessage = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMessage)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for", player.Name)
else
warn("Unable to save for", player.Name)
end
end
end
Players.PlayerRemoving:Connect(PlayerRemoving)
function ServerShutdown()
if RunService:IsStudio() then
return
end
print("Server is shutting down, saving data!")
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
PlayerRemoving(player)
end)
end
end
game:BindToClose(ServerShutdown)
local function GetNightData(player, dataType)
if dataType and player then
local maxNightUnlocked = sessionData[player.UserId][dataType]
return maxNightUnlocked
else
warn("Player or DataType is invalid.")
return nil
end
end
requestNightDataEvent.OnServerInvoke = GetNightData