Calling functions when joining/leaving

hey so im trying to implement a saving system but the part where im having a problem is making it save when the character is being removed (when leaving) and i want to call a function when the character just spawned. please help

server script in serverscriptservice btw

local function saveAura(player)
	local profile = datamanager.Profiles[player]
	local aura = player.Character:FindFirstChild("Aura"):FindFirstChildWhichIsA("Folder")
	
	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)
	local aurafolder = player:WaitForChild("Aura")
	local currentaura = player:WaitForChild("CurrentAura")
	
	if currentaura.Value ~= "" then
		aurahandler.EquipAura(player, player.Character, game.ReplicatedStorage.Auras[currentaura.Value]) --aurahandler is a module script in replicatedstorage (the equipping function works so thats not the problem)
	end
end




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

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		equipOnJoin(player)
	end)
end)

currentaura is a value

i also get a orange message thing but idk what it means:
Infinite yield possible on ‘Players.Kyhomi:WaitForChild(“Aura”)’

also i gtg so ill see tomorrow, thankss

thats not a error.i call it warning.i also get that too with WaitForChild.i think it happens because the script cant find the object.

oh ok but it doesnt call the function when i leave (from now on i cant answer questions, sorry :frowning: )

you dont need the character removing function.try removing it.

nvm i can reply i just cant try them, so if u may have found the solution then you cab just tell me what to do on here (u can put multiple ideas for me to try them all)

kk im gonna try it tomorrow thanks

Maybe this …

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

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        task.wait(0.33) equipOnJoin(player)
    end)
    player.CharacterRemoving:Connect(function(char)
        saveAura(player)
    end)
end)

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

It won’t work when your player leaves on studio as the game is forced shutdown whenever you end a studio test session!

To counteract this, and this should be done in the first case in the scenario that your game has been shutdown and all players are removed!

game:BindToClose(function()
       for _, player in pairs(game.Players:GetPlayers()) do
              — logic for saving data
           end
        end)
1 Like

This warning occurs when an object has been yielded for too long with :WaitForChild().

It is best practise to NOT use :WaitForChild() on the server as all assets are loaded together on the client as opposed to the server.

Yes it would also be nice to have a game:BindToClose(function()
Along with it.

ohh ok thanks but what did i do wrong for the joining part in that case, other than the infinite yield is it correct or nah?

Considering this is a server script (I hope), you do not need to yield :WaitForChild() as I prematurely stated.

This error will constantly throw this every time the script is executed if the object is not found.

Is the object that you are explicitly waiting for ever created on behalf of the server?

well on another server script theres a function that spawns a folder named Aura and when an aura is equipped another folder spawns with the same name as the equipped aura

You could add in CharacterAdded

task.wait(0.33) equipOnJoin(player) 

to give the other script time to create the setup

ohh true true i didnt think about that lol

btw does bindtoclose and the other stuff mean that once i close the game on studio its gonna save for everyone? if so what if i want it to save for only the person thats leavng? do i just keep what i did and use the bindtoclose only for testing if it works?

:BindToClose() is a function that is called whenever the server is shutting down. Therefore, leading to all the players being disconnected. Hence we must run through all players data to save it.

ok so i did get that so i should only use it for testing in studio but use playerremoving in the actual game?

You should be using it for both studio and roblox.

There is a feature on Roblox where you can shutdown servers, this will cause the BindToClose() function to fire.

For further information, I suggest reading this.

oh ok but i need the function to only affect the person who left