For some reason this error, leaderstats is not a valid member of Player “Players.Audit_s”, keeps appearing in my script.
My script is in ServerScriptService:
For some reason this error, leaderstats is not a valid member of Player “Players.Audit_s”, keeps appearing in my script.
My script is in ServerScriptService:
Maybe you need to WaitForchild for leaderstats
Can you please show us the code you are using to create the leaderstats?
I have 2 places for this script: The one with many scripts for various unrelated features, and second for this feature exclusively. The second one loads the script 100% fine.
What I’ve noticed is happening is that the Players.leaderstats folder isn’t being created.
(I cant get this line of code into the code block for some reason)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cultcoin = Instance.new("IntValue")
cultcoin.Name = "CultCoin"
cultcoin.Value = 10000
cultcoin.Parent = leaderstats
local inventory = Instance.new("Folder")
inventory.Name = "PetInventory"
inventory.Parent = player
local equippedPet = Instance.new("StringValue")
equippedPet.Name = "EquippedPet"
equippedPet.Parent = player
player.CharacterAdded:Connect(function(char)
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
end
end)
equippedPet.Changed:Connect(function()
if equippedPet.Value ~= nil then
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
end
end
end)
If you go in game and test it, does the error still occur?
Only in the place that has scripts for unrelated things. I’ve copy-pasted all the scripts for this feature 1:1 from the isolated place they were at.
OK, my theory is probably correct then.
Basically, the player is added too quickly in Studio, such that the script which checks for when the player joins simply doesn’t have enough time to join.
Can you please define a seperate function for PlayerAdded, which is called when game.Players.PlayerAdded fires, and also has the following code below the connection:
for _, player in ipairs(game.Players:GetPlayers()) do
PlayerAdded(player)
end
Oh, I apologize. I was thinking of “in-game” as play test. The error does not occur in the actual game.
So, the issue is that my character gets added too quickly when I do a studio test?
Not neccessarily the character - more so the player Instance (although the issue can occur with the character do)
Should I add the code you provided me regardless of the error not appearing in the actual game?
Yeah.
“Play Here” can act the same as joining an actual game, at least for this issue.
I’m not very sure on how to do this. I’m completely new to coding and used tutorials to help me with everything ![]()
function PlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cultcoin = Instance.new("IntValue")
cultcoin.Name = "CultCoin"
cultcoin.Value = 10000
cultcoin.Parent = leaderstats
local inventory = Instance.new("Folder")
inventory.Name = "PetInventory"
inventory.Parent = player
local equippedPet = Instance.new("StringValue")
equippedPet.Name = "EquippedPet"
equippedPet.Parent = player
player.CharacterAdded:Connect(function(char)
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
end
end)
equippedPet.Changed:Connect(function()
if equippedPet.Value ~= nil then
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
end
end
end)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for _, player in ipairs(game.Players:GetPlayers()) do
PlayerAdded(player)
end
Why would you run the function on existing players…?
Because Roblox doesn’t always load the script fast enough for the player to be detected using PlayerAdded, when in Studio.
Thank you so much for your quick and useful help
This has fixed the error
It’s unnecessary. His code where the error occurred is a ClickDetector which means eventually leaderstats will exist. He just needs to have a check for if leaderstats exists.
I also don’t recommend spoon feeding new programmers. You’ve offered him a solution that does work, but really isn’t the best for him and his code.
It’s not unnecessary.
leaderstats will never exist because the PlayerAdded code wasn’t running. The solution I provided fixed that, and was the solution to the post.
I made sure to explain how to fix it prior, and only provided the code because they requested it.
I’m thinking that you are just looking for an argument at this point.