Hi,
I was messing around with Datastores and was able to create a script that I am pretty sure should work for saving player locations when they leave. Except, it doesn’t and I’ve looked around the devforum a bit and have gone insane from trying to figure this out. I know I can use bindToClose(), but I am wondering does that work for if one player leaves and there is still other players in the game?
I think this error has something to do with :SetAsync():
local MainServerModuleScript = {}
local ServerData = require(script.ServerData)
local PlayerModuleScript = require(script.PlayerModuleScript)
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("Playa")
MainServerModuleScript.__index = MainServerModuleScript
game.Players.PlayerAdded:Connect(function(player) -- This I am pretty sure works as intended
player.CharacterAdded:Wait()
local HRP = player.Character.HumanoidRootPart
local data
local success, err = pcall(function()
data = PlayerData:GetAsync(player.UserId)
end)
print(data)
if success and data then
HRP.Position = Vector3.new(data[1], data[2], data[3])
print("Player Has Data!")
else
print("Player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
-- This doesnt save when players leave, I know it might be because the server
-- is shutting down before but it can save I've tried leaving a game and rejoining
-- with still 2 players in the game and it still doesn't work
player.CharacterRemoving:Connect(function(character)
local playerLocation = {character.HumanoidRootPart.Position.X, character.HumanoidRootPart.Position.Y, character.HumanoidRootPart.Position.Z}
local success, err = pcall(function()
print("Test") -- this prints
PlayerData:SetAsync(player.UserId, playerLocation) -- I think this is the problem
print("SUCCESS?") -- this does not print
end)
if success then
print("User Data Saved")
else
print("User data not saved!!!")
warn(err)
end
end)
Thanks, Ultan