Stats folder not creating?

I’m working on a game and for the players stats sometimes they dont create, and sometimes they do.

game.Players.PlayerAdded:Connect(function(player)
	local statsFolder = Instance.new("Folder")
	statsFolder.Name = "stats"
	statsFolder.Parent = player
	
	local status = Instance.new("StringValue")
	status.Name = "Status"
	status.Parent = statsFolder
	status.Value = "Normal"
end)

Infinite yield possible on 'Players.mlnitoon2:WaitForChild("stats")'  -  Studio

Sometimes, on server startup, the player can be added before the PlayerAdded signal is connected (which means it gets completely skipped). What you could do to prevent this is make a for-loop that only runs once, that goes through each player and essentially does the exact same as your signal (creates the folder etc.)

1 Like

can you send a script example of this?

Sure, this would go underneath where you connect your signal:

for _,player in pairs(game:GetService("Players"):GetChildren()) do

    local statsFolder = Instance.new("Folder")
	statsFolder.Name = "stats"
	statsFolder.Parent = player
	
	local status = Instance.new("StringValue")
	status.Name = "Status"
	status.Parent = statsFolder
	status.Value = "Normal"

end

Basically it’s just running the same code you put in your signal once for all existing players that somehow didn’t get caught by it

Note: you may or may not need to add a short pause before running this loop, only one way to find out haha

okay i think that fixed it.
but now it gave me 2 new problems…

ok so like. apparently its a ‘fake’ player. but its just roblox studio being stoopid - Server - ServerHandler:35
23:25:40.502 nil - Server - ServerHandler:69
23:25:40.502 ServerScriptService.ServerHandler:70: attempt to index nil with ‘AssetId’ - Server - ServerHandler:70

local function RetrieveRandomGame(playerwhorolled)
	if playerwhorolled then
		local plrstats = playerwhorolled:FindFirstChild("dataStats")
		plrstats.Status.Value = "Rolling"
		local digits = {}
		local randomNum = Random.new()
		for i=1,10 do
			table.insert(digits, randomNum:NextInteger(0,11))
		end

		local PlaceId = tonumber(table.concat(digits))
		if PlaceId then 
			local Place = MarketPlaceService:GetProductInfo(PlaceId)
			local IsAGame = Place["AssetTypeId"] == 9 
			if Place and Place ~= {} and IsAGame then 
				if not string.find(Place["Name"], "'s Place", 1, true) then
					plrstats.Status.Value = "Roll"
					print(Place["AssetId"])
					return Place
				else
					return RetrieveRandomGame()
				end

			else
				return RetrieveRandomGame() 
			end
		else
			return RetrieveRandomGame()
		end
	else
		warn("ok so like. apparently its a 'fake' player. but its just roblox studio being stoopid")
	end
end
``` script for the first error.

```lua
ReplicatedStorage.RollPlace.OnServerEvent:Connect(function(player)
	print(player.Name)
	local randomGame = RetrieveRandomGame(player)
	print(randomGame)
	print(GetGameData("Visits", randomGame["AssetId"]))


	-- player.LatestRolledGameName = randomGame["Name"]
	--player.LatestRolledGameID = randomGame["AssetId"]
	--player.LatestRolledGameCreator = randomGame["Creator"]["Name"]
end)
``` script for the second error

Can you show me where you’re calling your RollPlace remote event? For some reason, it seems that the player being received by the server is nil and causing all those other problems

It’s on a client script called ClientHandler.

script.Parent.RollButton.Activated:Connect(function()
	game.ReplicatedStorage.RollPlace:FireServer()
end)

game.Players.LocalPlayer:WaitForChild("dataStats").Status:GetPropertyChangedSignal("Value"):Connect(function()
	script.Parent.RollButton.Text = game.Players.LocalPlayer.dataStats.Status.Value
end)

your localscript seems fine, which makes this a much more bizarre issue lol

when you did print(player.Name), did you get an error?

nope. it printed my players name

Wait a minute. I just had a revelation.

Inside your RetrieveRandomGame() function, you are sometimes recursively calling it without including the player as a parameter. That is almost certainly the reason that it’s nil

Thank you, I don’t know how I didn’t notice that :sob:

2 Likes

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