Calling functions when joining/leaving

Your PlayerRemoving function will handle that, the BindToClose() function will fire whenever a studio session completes or the game is shutdown.

If you want a more in depth guide to saving data in data stores, I recommend referring to this.

1 Like

This explains everything regarding BindToClose() and .PlayerRemoving which is what you will need in your case of saving data.

oh i get it now thanks i have a lot of stuff to try tomorrow

If you find that this works, please mark the topic as solved!

I best be off to sleep now.
Farewell.

i cant try them right now so idk if its gonna work but cya lol

ohh wait im stupid, the reason why the player:waitforchild(“aura”) doesnt work is because i looked for it in the player, not the character lol

ok, so here is the script with the equiponjoin now working, but not the saveaura as a get this error:

attempt to index nil with ‘FindFirstChild’

local aura = player.Character:FindFirstChild("Aura"):FindFirstChildWhichIsA("Folder")

new script with equiponjoin working:

local function saveAura(player)
	local profile = datamanager.Profiles[player]
	local aura = player.Character:FindFirstChild("Aura"):FindFirstChildWhichIsA("Folder") --where i get the error
	
	if aura then
		profile.Data.CurrentAura = aura.Name
		player:FindFirstChild("CurrentAura").Value = profile.Data.CurrentAura
		print("Successfully saved the aura "..aura.Name.." to "..player.Name..".")
	else
		profile.Data.CurrentAura = ""
		player:FindFirstChild("CurrentAura").Value = profile.Data.CurrentAura
	end
end

local function equipOnJoin(player, char) --this works
	local aurafolder = char:WaitForChild("Aura")
	local currentaura = player:WaitForChild("CurrentAura")
	
	if currentaura.Value ~= "" then
		aurahandler.EquipAura(player, player.Character, game.ReplicatedStorage.Auras[currentaura.Value])
	end
end




game.Players.PlayerRemoving:Connect(function(player)
	saveAura(player)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		saveAura(player)
	end
end)

game.Players.PlayerAdded:Connect(function(player) --this works
	player.CharacterAdded:Connect(function(char)
		equipOnJoin(player, char)
	end)
end)

what am i doing wrong? ik its because the game cant find the character but how can i make it work?

This means that the Character does not exist therefore the :FindFirstChild() cannot call if there is nothing to call from.

To fix this issue, try and ensure that the players’ character exists and is already loaded. You can do this by:

local character = player.Character or player.CharacterAdded:Wait()

Let me know if this has helped!

but the function is called when the player is leaving, so how can i wait for it to load when its leaving?

Which line is throwing this error?

this line in saveaura:

local aura = player.Character:FindFirstChild("Aura"):FindFirstChildWhichIsA("Folder")

wait what if i save everytime another aura is equipped? wouldnt that work?

I would suggest attempting my previous suggestion to see if that alters the outcome.

alright ill try your idea first

so unfortunately it didnt save but i didnt get any errors

How are you sure it didnt save?

it kept the old value when i rejoined

Have you got a loading script for when the player joins the game?

i dont know what that means so nope

Do you load the saved data when a player joins?