PlayerAdded is not creating my instance.new

wait(3)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = ReplicatedStorage:WaitForChild("Tools")
local CraftTool = ReplicatedStorage:WaitForChild("CraftTool")
local craftingInfo = require(ReplicatedStorage:WaitForChild("CraftingInfo"))

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Wood = script.Parent["Inventory"]:WaitForChild("Wood") 
	Wood.Parent.Parent = leaderstats
	
	local Rock = script.Parent["Inventory"]:WaitForChild("Rock")
	Rock.Parent.Parent = leaderstats
	
	local Metal = script.Parent["Inventory"]:WaitForChild("Metal")
	Metal.Parent.Parent = leaderstats
	
end)

CraftTool.OnServerInvoke = function(player, toolName)
local leaderstats = player.leaderstats
	local crafted = false
	
	for i, v in pairs(craftingInfo.toolName) do
		local material = leaderstats:FindFirstChild(i)
		if material then 
			if material.Value >= v then
				material.Value = material.Value - v
				crafted = true
			else 
				crafted = false
			end
		end
	end
	if crafted == true then
		local tool  = Tools:FindFirstChild(toolName):Clone()
		
		if tool then
			tool.Parent = player.Backpack
		end
	end
end

I have this code, which should be defining leaderstats as Instance.new, yet line 24 which calls leaderstats under an ingame function says that it is not a valid member of Players.UltraConstructor6 (my PlayerAdded.)

I’ve tried looking up similar forums, but most of them was the issue of lack of definition. I’m pretty sure I defined this one correctly, but…

Note: when searching in the explorer after starting the game, leaderstats does NOT show up!

What’s the issue here?

3 Likes

I highly suggest that you loop through every player in the game before listening to the PlayerAdded event, as the slightest delay can cause it not to fire, especially in studio as the player is created before the script.

local GetPlayers = Players:GetPlayers()
for _, Player in ipairs(GetPlayers) do
    task.spawn(PlayerAdded, Player)
end

Players.PlayerAdded:Connect(PlayerAddedFunction)
2 Likes

When you try this in studio it wouldn’t create a leaderstats folder because you have that wait() at the top. Essentially when you connect to the game it would be yielding the script and it wouldn’t detect you joining the game.

4 Likes

Adding onto what you’ve said, I’d like to say that adding waits on top of scripts is a terrible idea. There is almost no reason to why anything would need that. It’s just the lazy way of doing things, there are better ways to wait for necessary things. This explains it really well. An arbitrary wait like wait(3) is even worse than the repeat wait() until mentioned there.

Additionally, task.wait is almost always better that wait.

3 Likes

How do I integrate this?

I read up on it. How does it help this code?

Something really odd’s happening- i get different errors with or without the wait! Trying to figure it out rn.

1 Like

the issues that I can see is the wait(3) at the top of the script, it might be yielding it too long, as well as sometimes .PlayerAdded isnt picked up inside of studio

1 Like

If you have something yielding the thread like your wait() is doing, then the event listener for the PlayerAdded signal will run after, skipping the first players that joined the server during that yield time.

Even with nothing seemingly yielding, there is still a rare chance that something with the server instancing lagged the script to execute the listener after the player joined.

Use it because it’s generally good practice to do so:

Example setup:

--!strict


local PLAYERS: any = game:GetService("Players")


local function playerSetup(player: any): ()
	--Handle your player instancing here.
end


for _, player in ipairs(PLAYERS:GetPlayers()) do
	task.spawn(playerSetup, player)
end

PLAYERS.PlayerAdded:Connect(playerSetup)
1 Like

This is something really common: 2 errors but one shadows the other. Keep going without the wait as that fixes one issue. What is the new error?

1 Like

Inventory is not a valid member of ServerScriptService

Referring to lines 13, 16, and 19.

1 Like

You’re going to have to change the “script.Parent” on lines 13, 16 and 19 to “player” , the player argument you’ve set.

2 Likes

I tried making the changes you suggested, and they took away all the errors! Thank you. if you have the time, could I ask you another question?

1 Like