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!
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)
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.
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.
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
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)