How to find a folder within a player?

Hello,

I’m not that good with scripting. My problem is trying to define my “leaderstats” folder found with every player. This is my code for creating the currency:

game.Players.PlayerAdded:connect(function(player)
	local player = game.Players.LocalPlayer
	local f = player:FindFirstChild("leaderstats")
	local Coins = Instance.new("IntValue", f)
	Coins.Name = "Coins"
end)

This code although I believe should work, brings up the error message:

13:11:20.099 - ServerScriptService.Coins:3: attempt to index nil with ‘FindFirstChild’

I use multiple currencies in my game, but create the actual folder in this line of code here:

local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Rank = Instance.new("IntValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats

The code creating the folder is stuck inside a different script because of the uses within both.

I’ve tried looking for other code from my project to fix this issue, but haven’t gotten any useful code I could use. Would somebody be able to tell me why this code isn’t working correctly? I’d like to know how to fix this in the future.

Thanks!
Dailychips123

You are trying to get a LocalPlayer from the serverside which is not possible.

game.Players.PlayerAdded:connect(function(player)
	local f = player:FindFirstChild("leaderstats")
	local Coins = Instance.new("IntValue", f)
	Coins.Name = "Coins"
end)

This should work.

2 Likes

I suggest making the Rank,Coins and any other currency one script Instead of separate scripts.

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Rank = Instance.new("IntValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats	
end)

Sorry to question you again, but while I’m using my data store for my Rank saving, I have this line of script giving me the same error:

Rank.Value = data

(The data is the sent count to the data store)
Here is the full line of code for anything more:

local DSS = game:GetService("DataStoreService")
local RankSave = DSS:GetDataStore("RankSave")

game.Players.PlayerAdded:Connect(function(player)
	local f = player:FindFirstChild("leaderstats")
	local Rank = player:FindFirstChild("Rank")
	
	local data
	local success, errormessage = pcall(function()
		data = RankSave:GetAsync(player.UserId.."-Rank")
	end)
	
	if success then
		Rank.Value = data
	else
		print("There was an error in getting your rank! Contact a developer for help.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		RankSave:SetAsync(player.UserId.."-Rank",player.leaderstats.Rank.Value)
	end)
	
	if success then
		print("Your data was loaded correctly.")
	else
		print("There was an error in getting your rank! Contact a developer for help.")
		warn(errormessage)
	end
end)

I tried using your code you sent me by changing it, but it didn’t work either way.

Could you fix this error?

This should work:

local DSS = game:GetService("DataStoreService")
local RankSave = DSS:GetDataStore("RankSave")

game.Players.PlayerAdded:Connect(function(player)
	local f = player:FindFirstChild("leaderstats")
	local Rank = player:FindFirstChild("Rank")
	
	local data = nil
	local success, errormessage = pcall(function()
		data = RankSave:GetAsync(player.UserId.."-Rank")
	end)
	
	if data ~= nil then
		Rank.Value = data
	else
		print("There was an error in getting your rank! Contact a developer for help.")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		RankSave:SetAsync(player.UserId.."-Rank",player.leaderstats.Rank.Value)
	end)
	
	if success then
		print("Your data was loaded correctly.")
	else
		print("There was an error in getting your rank! Contact a developer for help.")
		warn(errormessage)
	end
end)

Not sure if you kept this part out of the code but you need game.BindToClose for the datastore to work.

Nil

Also, it didn’t print until after I closed the game.

Why are you not creating the folder and the values in the same script? They way you are going about scripting it is not effective nor efficient.

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Rank = Instance.new("IntValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats	
end)

This is the script I’m using that you are asking about.
The data store script has the same error, the line is still broken.

EDIT: I added the part to my script, it did work finally! Thank you!

local DSS = game:GetService("DataStoreService")
local RankSave = DSS:GetDataStore("RankSave")
local CoinsSave = DSS:GetDataStore("CoinsSave")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Rank = Instance.new("IntValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats	
	
	local CoinsData = nil
	local RankData = nil
	
	pcall(function()
		CoinsData = CoinsSave:GetAsync(player.UserId.."-Coins")
	end)
	
	pcall(function()
		RankData = RankSave:GetAsync(player.UserId.."-Rank")
	end)
	
	if CoinsData ~= nil then
		Coins.Value = CoinsData
	end	
	
	if RankData ~= nil then
		Rank.Value = RankData
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	CoinsSave:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
	RankSave:SetAsync(player.UserId.."-Rank",player.leaderstats.Rank.Value)
end)

game.BindToClose:Connect(function()
	for i, Player in pairs(game.Players:GetPlayers()) do
		Player:Kick()
	end
	wait(5)
end)