hello all, I’ve been looking at this code block here and i am so beyond lost why not even the output “data saved” is showing up. i tested in studio, in game by looking at the output, and its just not even printing. Can someone explain a reason why it wouldn’t be functioning so I can go in my code and fix it? I don’t even know where to look
game.Players.PlayerRemoving:Connect(function(player)
print("data saved")
local playerKey = "player"..player.UserId
local data = player.leaderstats.Wins.Value
dataStore:SetAsync(playerKey, data)
end)
It depends on what type of script you’re trying to run this through:
LocalScripts can only run that’s valid to the Player/Client side
Scripts can only run prior if they’re parented inside the workspace/ServerScriptService
ModuleScripts & their functions will only run if you’ve called them at the exact right time (Although it doesn’t look like this is one)
Also make sure to turn on API Services if you’re wanting to use the Datastore functions
Use a Script, inside ServerScriptService if you want this to work
game.Players.PlayerRemoving:Connect(function(Plr)
print("Test")
local UserId = Plr.UserId
local Wins = Plr:WaitForChild("leaderstats"):WaitForChild("Wins")
local success, whoops = pcall(function()
dataStore:SetAsync("player"..UserId, Wins.Value)
end)
if success then
print("Player's Data for", Plr, "has been saved!")
else
warn("Error saving data:", whoops)
end
end)
Probably this code is not run, does a print statement outside it do anything? Maybe it is in a modulescript that is never imported, or in a function that is never called? Naturally to see it in game, you need to have another person leave to be able to see the message, but I guess you did that.
got it! I put a print statement outside to test and apparently the script was never even making it to the part. my guess was because it was after a while true do statement, which I always thought wouldn’t stop the rest of the script from running but in hindsight that makes a lot of sense. Thank you very much!
You don’t need to use a pcall because most functions won’t result back as errors unless if you’re handling them the wrong way
You should only use pcalls if a function you’re using is uncertain about it’s usage, and there may be the potential chance of something breaking & entirely ruining the Script (Such as Datastore function fails, or something unexpectingly returning back as nil)