Hi! So for my datastore, im trying to save the value of the player’s nametag’s title to previoustitle. But, it is giving me this error:
With this script:
local function saveData(plrLeaving)
if not plrLeaving:GetAttribute("DataSuccess") then
return
end
local ownedTools = {}
for _, tool in pairs(plrLeaving.OwnedTools:GetChildren()) do
table.insert(ownedTools, tool.Name)
end
local ownedTitles = {}
for _, title in pairs(plrLeaving.OwnedTitles:GetChildren()) do
table.insert(ownedTitles, title.Name)
end
local ownedStyles = {}
for _, style in pairs(plrLeaving.OwnedStyles:GetChildren()) do
table.insert(ownedStyles, style.Name)
end
local previousTitle = plrLeaving.Character.Head.NameTag.Title.Text
warn("THE CURRENT TITLE IS: "..previousTitle) -- CHECK IF DOES PRINT AN ORANGE TEXT WHENEVER YOU LEAVE
local output = {
Titles = ownedTitles,
Tools = ownedTools,
Styles = ownedStyles,
Coins = plrLeaving.leaderstats.Coins.Value,
Wins = plrLeaving.leaderstats.Wins.Value,
Skips = plrLeaving.leaderstats.Skips.Value,
Style = "", -- BE SURE TO SAVE THE STYLE!
Title = previousTitle
}
local success, err = pcall(function()
ds:SetAsync("player_"..plrLeaving.UserId, output)
end)
end
Specifically, this line:
local previousTitle = plrLeaving.Character.Head.NameTag.Title.Text
Can anybody help me?
It’s saying that head doesn’t exist possibly because the player is leaving to fast…
Please add this function below the Player reamoving event…
Note: Please add saving (Copy paste the saving from Player removing) in BindToCloseEvent after looping through the player
Also, game:BindToClose() will not shut down the Server too fast meaning the Player will not leave too fast.
One more thing don’t make it more than 5 Seconds it will crash the Players.
function OnShutDown()
for i,Player in pairs(game.Players:GetPlayers()) do
-- save the stuff here
task.wait(4) -- How many seconds until the Server Shuts down
end
end
game:BindToClose(OnShutDown)
Im confused. Replace this with saveData? Do I use the code from saveData for the OnShutDown function?
Don’t remove the Player leaving event, Just copy paste the Saving code from the Player leaving event into the ShutDown function…
Please check this to understand more. @romeanyguy10
Why does it say until the server shuts down?
When there is 1 Player left in the game and the Player leaves the server will just shut down meaning there will be no time saving the data. (Especially with studio).
That’s why we use this event it will wait a bit until the Player data will be saved.
Also, it isn’t working. Im still getting the same error from the same line of code in saveData.
Oh ok. This makes a lot of sense.
Also change this
local previousTitle = plrLeaving.Character.Head.NameTag.Title.Text
To This
local previousTitle = plrLeaving.Character:FindFirstChild("Head").NameTag.Title.Text
In both saveData and OnShutDown?
It is now saying: attempt to index nil with ‘FindFirstChild’ .
This should work before the head do that
repeat until Player.Character
local previousTitle = plrLeaving.Character.Head.NameTag.Title.Text
In both saveData and OnShutDown?
Now it says: Script timeout: exhausted allowed execution time. And it crashes studio. I put it in exactly how you wrote it.
Oh, I know Sorry it’s just repeating too fast (Causing the script to crash) add a task.wait() like this
repeat task.wait(.2) until Player.Character
local previousTitle = plrLeaving.Character.Head.NameTag.Title.Text
Thank you, this got rid of the error. Unfortunately, leaderstats and previoustitle are not saving which makes this useless.