Function not running so script not working

  1. What do you want to achieve? Making the script assign an attribute to a character when they join the game.

  2. What is the issue? When I run the script in Studio line 4-16 get skipped.

  3. What solutions have you tried so far? I have tried using the AI which did not fix my issue.

I am pretty new to scripting so if it is a very dumb mistake let me know,

local Players = game:GetService("Players")
print("Test1")

local function onPlayerAdded(Player)
	print("Test2")
	if Player:IsInGroup(17326955) then
		print("Test3")
		Player:SetAttribute("IsInGOC", true)
		print("Player in GOC")
		local rank = Player:GetRankInGroup(17326955)
		Player:SetAttribute("GOCRank", rank)
	else
		Player:SetAttribute("IsInGOC", false)
		print("Player not in GOC")
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
print("GOC Main Group Membership Check Complete")

Hey Blocks,

Try adding this to the top of the script and see if that does anything.

task.wait(1)

Just copy and pasted the script into studio and it seems to be working fine for me, your script is probably (for some reason) not running fast enough so the player loads in before playeradded has the chance to even fire, try publishing the game and have a friend or alt account join and monitor the dev console to see if the statements print.

Thought I might also add I put it as a localscript in StarterCharacter.

I see the issue then, so yeah the script isn’t loading fast enough for playeradded to be able to fire, I would recommend putting this in a normal script in serverscriptservice to fix the issue.

Alright, I was planning to have it as an attribute under every character in the workspace so I put it there.

That’s the problem. The code needs to be in a server script in serverscriptservice.

I understand the confusion, the server can do what you want perfectly fine and it should honestly, theres no need to have it in a localscript under StarterCharacter.

I was just explaining why I did what I did from the start.

PlayerAdded doesn’t run in studio, just make a part at the end of your script that reads

game.RunService:IsStudio() do
 for i, player in pairs(game.Players:GetPlayers()) do
  --// code
 end
end

No, that’s not right. PlayerAdded does fire in Studio, the problem might be to do with @nuckehh 's installation of Studio.

Try reinstalling Studio, and try it on the actual Roblox game client (with the script being a server Script in ServerScriptService.

1 Like

it’s not, when you have large scripts playeradded will runtime and you’ll join before it fires

Oh, I didn’t know that. But surely, that would mean adding a task.wait(1) statement at the top of the script would fix it?

Thanks for the missing info anyway :smiley:

not really, cause you could join before the wait is finished but the problem here is that he’s got it in a local script under starter character (i did not read the whole question)

If it’s a local script then try this:

local Player = game:GetService("Players").LocalPlayer
print("Test1")

local function onPlayerAdded(Player)
	print("Test2")
	if Player:IsInGroup(17326955) then
		print("Test3")
		Player:SetAttribute("IsInGOC", true)
		print("Player in GOC")
		local rank = Player:GetRankInGroup(17326955)
		Player:SetAttribute("GOCRank", rank)
	else
		Player:SetAttribute("IsInGOC", false)
		print("Player not in GOC")
	end
end

onPlayerAdded(Player)
print("GOC Main Group Membership Check Complete")

See if doing this fixes the problem:

local Players = game:GetService("Players")
print("Test1")

local function onPlayerAdded(Player)
	print("Test2")
	if Player:IsInGroup(17326955) then
		print("Test3")
		Player:SetAttribute("IsInGOC", true)
		print("Player in GOC")
		local rank = Player:GetRankInGroup(17326955)
		Player:SetAttribute("GOCRank", rank)
	else
		Player:SetAttribute("IsInGOC", false)
		print("Player not in GOC")
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
for _, Player in Players:GetPlayers() do onPlayerAdded(Player) end
print("GOC Main Group Membership Check Complete")

If it doesn’t work then there must be something else that preventing the script from working

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