How to get a value from a folder before a player leaves

I am stuck on this issue, and I can’t figure out how I will solve it.

game.Players.PlayerRemoving:Connect(function(player)
	local folder = game.ReplicatedStorage:WaitForChild(player.Name) --I didnt use findfirstchild cus of attempt to index nil
	local id = player.UserId
	local tool = folder:FindFirstChildOfClass("StringValue").Value
	print(tool)
end)

Nothing will print in the output.

2 Likes

Are you sure it parents the folder to replicated storage?

1 Like

Are you searching in the ReplicatedStorage for a folder named (player.name)?
Try putting print statements before each line like

game.Players.PlayerRemoving:Connect(function(player)
    print("Removing ", player.Name)
	local folder = game.ReplicatedStorage:WaitForChild(player.Name)
    print(folder)
	local id = player.UserId  --what is this line for?
	local tool = folder:FindFirstChildOfClass("StringValue").Value
	print(tool)
end)
1 Like

Here is a solution that worked for me.
image

--> Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--> Functions
local function PlayerAdded(player: Player) --> Create The Folder
	local Folder = Instance.new("Folder")
	Folder.Name = player.Name
	
	local Tool = Instance.new("StringValue")
	Tool.Name = "Tool"
	Tool.Value = "Tool"
	
	Tool.Parent = Folder
	
	Folder.Parent = ReplicatedStorage
end

local function PlayerRemoving(player: Player) --> Get The Folder & Tool
	local Folder = ReplicatedStorage:FindFirstChild(player.Name)
	local Tool = Folder:FindFirstChild("Tool")
	
	print(Tool.Value)
end

for _, v in pairs(Players:GetPlayers()) do
	PlayerAdded(v)
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

It could be race conditions and the script running after the first player joins the game so it doesnt create the folder, so thats why you always wanna loop through all the current players before you connect PlayerAdded

2 Likes

Yes. It’s a cloned folder but it is also parented to Replicated Storage

1 Like

Sorry line 5 was line for a data script I recently used. Thanks I will try this.

1 Like

Alright one question tho what does this do

local function PlayerRemoving(player: Player)

Why did you add a colon?

1 Like

Error: attempt to index nil with FindFirstChildOfClass

1 Like

when you make a variable and you do : Player for example, its telling the script that the variable is a type of Player.

local player: Player = player --> This Would Be A Player.
local x: number = 0 --> This Would Be A Number
2 Likes

I see thanks.

local function PlayerRemoving(player: Player) --> Get The Folder & Tool
	local Folder = ReplicatedStorage:FindFirstChild(player.Name)
	local Tool = Folder:FindFirstChild("Tool")

This didn’t work because player.Name was never assigned?

1 Like

Do you have another variable outside of that function that is called “player”?

No, I just copied your script.

Add a print(player) before the variable Folder and see what that prints

Ok it works now, but when I try connecting it to the folder I made by custom, it won’t work.

What do you mean the folder you made by custom?

Is player.name referencing the path to the player and then the name property? If so, the problem could be from you trying to find children inside a name.

local folder = game.ReplicatedStorage:WaitForChild(player.Name) -- Name Is String, or Child?
local tool = folder:FindFirstChildOfClass("StringValue").Value -- Trying to find children through name

It’s working now but thanks too!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.