local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local keybindsData = require(ReplicatedStorage.Modules.Data.Keybinds)
local gameSettingsData = require(ReplicatedStorage.Modules.Data.GameSettings)
local remoteEvents = ReplicatedStorage.RemoteEvents
local keybindsDataStore = DataStoreService:GetDataStore("Keybinds")
local settingsDataStore = DataStoreService:GetDataStore("GameSettings")
local function onPlayerAdded(player: Player)
local userId = player.UserId
local success, keybinds = pcall(function()
return keybindsDataStore:GetAsync(userId)
end)
print(success, keybinds)
if not success or not keybinds then
keybinds = keybindsData:convertToString(keybindsData.default)
warn("REVERTING TO DEFAULT KEYS")
end
remoteEvents.SetKeybinds:FireClient(player, keybinds)
local success, gameSettings = pcall(function()
return settingsDataStore:GetAsync(userId)
end)
print(success, gameSettings)
if not success or not gameSettings then
gameSettings = gameSettingsData.default
warn("REVERTING TO DEFAULT SETTINGS")
end
remoteEvents.SetSettings:FireClient(player, gameSettings)
end
local function saveKeybinds(player: Player, keybinds)
print(player, keybinds)
local convertedKeybinds = keybindsData:convertToString(keybinds)
print(convertedKeybinds)
local success = pcall(function()
keybindsDataStore:SetAsync(player.UserId, convertedKeybinds)
print(keybindsDataStore:GetAsync(player.UserId))
end)
print(success)
end
local function saveSettings(player: Player, gameSettings)
print(player, gameSettings)
local success = pcall(function()
settingsDataStore:SetAsync(player.UserId, gameSettings)
end)
print(success)
end
game:BindToClose(function()
remoteEvents.SaveKeybinds:FireAllClients()
remoteEvents.SaveSettings:FireAllClients()
end)
Players.PlayerAdded:Connect(onPlayerAdded)
remoteEvents.SaveKeybinds.OnServerEvent:Connect(saveKeybinds)
remoteEvents.SaveSettings.OnServerEvent:Connect(saveSettings)
Are you the only player in the server? Is it possible that the server is ending before the events are processed? You might need to use BindToClose to get the events to process reliably.
You can test if this is the problem by seeing if:
game:BindToClose(function()
print("Stalling server closing.")
task.wait(5)
print("Server closing now.")
end)
prevents your problem. This should stall the server from ending for 5 seconds. I wouldn’t personally recommend this because it’s not guaranteed, but realistically all events like that should be processed in 5 seconds so in reality it’s fine.
I saw that u used the PlayerRemoving event from the Players Instance on a client sided script trying to see if the localplayer left. I think saving it regularly would fix it?
Yeah, I was thinking of doing this kind of system which saves it every minute or so, I’m not sure how it would work though because what if players just left before the 60 seconds? Im looking for a solution that makes it work for the player leaving
Okay, i’ll try implementing remote events for the :BindToClose(), do you think this “yielding” would also work for a playerRemoving event on the server?
Yes, or when the server shutdowns, but I think this yielding idea would work for a playerRemoving event, since remoteFunctions apparently yield like he said
yes, in that case you wouldnt need to iterate over every player (since theres only one)
but server shutdowns and any similar event that remove players in bulk would have players iterated over
and if youre concerned about the player not being there, any references to the player object are kept even after they leave, so that you can get any data needed.
the issue is that the client is trying to fire the server after the player is gone, which won’t work
if it does work somehow (or if im wrong), then you won’t see any of the effect unless you’re in studio and can access the console after server shutdown
i would put the playerRemoving event onto the server and put the functionality there