"Isn't a valid member of folder" Error

The error in the bottom corner is my issue, It’s not a valid member of the folder but it clearly is there under my player, The script below is the folder, I am trying to get both stats on this UI. I have no idea on how to fix this after searching up solutions, any Ideas?

local DataStore = game:GetService("DataStoreService")
--spills
local ds = DataStore:GetDataStore("PlayTime")
local dsb = DataStore:GetDataStore("SpillCollection")

game.Players.PlayerAdded:connect(function(player)
 	local leader = Instance.new("Folder",player)
	leader.Name = "Stats"
	

	--spills
 	local Time = Instance.new("IntValue",leader)
	Time.Name = "TimePlayed"
	Time.Value = ds:GetAsync(player.UserId) or 0
	ds:SetAsync(player.UserId, Time.Value)
	local canSave = true
	Time.Changed:connect(function()
		if canSave == true then
			canSave = false
			wait(5)
			ds:SetAsync(player.UserId, Time.Value)
			canSave = true
		end
		local WorkerPointt = Time.Value
	end)
	
	--spills
	local Spill = Instance.new("IntValue",leader)
	Spill.Name = "SpillsCleaned"
	Spill.Value = dsb:GetAsync(player.UserId) or 0
	dsb:SetAsync(player.UserId, Spill.Value)
	local canSave = true
	Spill.Changed:connect(function()
		if canSave == true then
			canSave = false
			wait(5)
			dsb:SetAsync(player.UserId, Spill.Value)
			canSave = true
		end
		local WorkerPointtt = Spill.Value
	end)
	
end)
game.Players.PlayerRemoving:connect(function(player)
	
	--spills
	ds:SetAsync(player.UserId, player.Stats.TimePlayed.Value)
	dsb:SetAsync(player.UserId, player.Stats.SpillsCleaned.Value)
end)

This code works with leaderstats, as it is fine in my main game.

There doesn’t seem to be any issue in this script, as it’s erroring on line 2 but line 2 in this script is a comment, can we see the LocalScript that is erroring?

while true do
	script.Parent.Text = game.Players.LocalPlayer.Stats.SpillsCleaned.Value
	wait(1)
end

This is inside the Textlabel although works perfectly fine for the Times Played Label.

Probably the SpillsCleaned value hasn’t existed by the time the scirpt was ran, change it to this?

local stats = game.Players.LocalPlayer:WaitForChild("Stats")
local spills = stats:WaitForChild("SpillsCleaned")

while true do
	script.Parent.Text = spills.Value
	wait(1)
end

Also you really should use the .Changed event to update the Text to the new value of the IntValue since using a while true do is not really efficient

1 Like