Script not creating folder upon PlayerAdded?

I have this script that barely fires PlayerAdded for some reason. I tried using a task.wait but it doesn’t work. Since the created objects are important for gameplay. The game becomes playable with lots of error printing. Is there any way to fix this?

Players.PlayerAdded:Connect(function(Player)
	local __NEW = script:WaitForChild("__PLRDATA"):Clone()
	__NEW.Parent = ReplicatedStorage:WaitForChild("__PLAYERDATA")
	__NEW.Name = Player.Name
	local __MISC = Instance.new("Folder")
	__MISC.Name = "Stats"
	__MISC.Parent = Player
	local __KS = Instance.new("NumberValue")
	__KS.Name = "KillStreak"
	__KS.Parent = __MISC
	warn(Player.Name.. " JOINED.")
	--__BACKUPEVENT:Fire(Player.Name)
	local __STATS = script:WaitForChild("Stats"):Clone()
	__STATS.Name = Player.Name 
	__STATS.Parent = ReplicatedStorage
end)

Try this:

local onPlayerAdded = function(Player)
	local __NEW = script:WaitForChild("__PLRDATA"):Clone()
	__NEW.Parent = ReplicatedStorage:WaitForChild("__PLAYERDATA")
	__NEW.Name = Player.Name
	local __MISC = Instance.new("Folder")
	__MISC.Name = "Stats"
	__MISC.Parent = Player
	local __KS = Instance.new("NumberValue")
	__KS.Name = "KillStreak"
	__KS.Parent = __MISC
	warn(Player.Name.. " JOINED.")
	--__BACKUPEVENT:Fire(Player.Name)
	local __STATS = script:WaitForChild("Stats"):Clone()
	__STATS.Name = Player.Name 
	__STATS.Parent = ReplicatedStorage
end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

Explanation:

1 Like

The problem with this is, i reference the created objects in the rest of the script so if i tried that solution they would all appear as unknown.

image

Also forgot to mention that only some of the instances don’t get created such as __STATS don’t get cloned but __MISC gets created. Any ideas on why this happens?

Check what is happening with wait for child it is probably the issue. You can also infer from the line order if the previous lines worked but not this line.

Either output from the function using return to get the newly created objects or put those connections within the player added function. TL;DR reorganize your script.

I did replace waitforchild with findfirstchild and it still wouldn’t run.

I’ll try that.

Actually nvm found the issue, it was a modulescript i was requiring at the beginning.

local __ENGINES = Modules:FindFirstChild("Engine")
local __ENGINE = require(__ENGINES)

Why is a modulescript yielding the entire script? and is there anyways to require it without yielding since it’s important for the script too. (as name says)

You should be able to solve this on your own, as you should be familiar with your own work to identify the source of yielding.

Otherwise to avoid yielding you can use task.spawn though it’s not recommended, as you would have to wrap it around a function and you cannot access the variables inside it.

task.spawn(function()
local __ENGINES = Modules:FindFirstChild("Engine")
local __ENGINE = require(__ENGINES)
end)

Yeah that’s what i did.

local __ENGINE 
task.spawn(function()
	 __ENGINE = require(__ENGINES)
end)

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