game.Players.PlayerAdded code isn't running

  1. What do you want to achieve? For my script to work and not fail.

  2. What is the issue? The code is not running at all, not even printing anything

  3. What solutions have you tried so far? It seems nobody has made a topic about this.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local rs = game:GetService("ReplicatedStorage")
local chatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
game:GetService("Players").PlayerAdded:Connect(function(plr)
	local speaker = chatService:GetSpeaker(plr.Name)
	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Parent = plr
	print('why isnt this working')
	canHurt.Value = false
end)

It’s located in ServerScriptService and is a server script.
The code doesn’t seem to be running…
No errors, it’s just not running.

The PlayerAdded event wont run in studio. I would assume for api reasons or metrics, something like that, it won’t work with studio.

1 Like

For me, that event has always runned.

Same here, I noticed this the other week, but from then on it wouldn’t run.

Make sure the script is enabled. Also make sure that Studio access to API services is enabled. I think.

You are requiring a module which takes enough time to for you to join without the event firing.

I just tried in ROBLOX Player and it started working.

1 Like

I removed the chatservice stuff (only in studio) and it worked.

You can easily fix this issue, just loop through the players and run code for each player:

local rs = game:GetService("ReplicatedStorage")
local chatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
game:GetService("Players").PlayerAdded:Connect(function(plr)
	local speaker = chatService:GetSpeaker(plr.Name)
	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Parent = plr
	print('why isnt this working')
	canHurt.Value = false
end)

for _, player in ipairs(game.Players:GetPlayers()) do
local speaker = chatService:GetSpeaker(plr.Name)
	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Parent = plr
	print('why isnt this working')
	canHurt.Value = false
end
1 Like

Incorrect, PlayerAdded will fire in Studio, sometimes the event is connected late whenever you join, which you can easily account for.

@BuilderBob25620, This is incorrect. API services have nothing to do with this.

My bad. I forgot that that is used for stuff like DataStores. Anyways, there is no reason for this event to not be firing.

This is the full script:

local rs = game:GetService("ReplicatedStorage")
local chatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
game:GetService("Players").PlayerAdded:Connect(function(plr)
	local speaker = chatService:GetSpeaker(plr.Name)
	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Parent = plr
	print('why isnt this working')
	canHurt.Value = false
	local cooldown = Instance.new("NumberValue")
	cooldown.Name = "cooldown"
	cooldown.Parent = plr
	local mps = game:GetService("MarketplaceService")
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		cooldown.Value = 35
		plr:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("angry"):WaitForChild("cooldowntime").Text = "35 Second Cooldown"

	else
		cooldown.Value = 40
	end
	-- V.I.P
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		local char = plr.Character or plr.CharacterAdded:Wait()
		rs:WaitForChild("VIP"):Clone().Parent = char:WaitForChild("Head")
		speaker:SetExtraData("Tags",{{TagText = "👑V.I.P", TagColor = Color3.fromRGB(234, 255, 0)}})
		char:WaitForChild("Humanoid").WalkSpeed = 36
	end
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		game:GetService("ServerStorage"):WaitForChild("boombox"):Clone().Parent = plr:WaitForChild('Backpack')
	end
end)

Just do something like this:

-- Services --
local ps = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local chatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

-- Variables --
local id = 0; -- Your id here

-- Functions --
function SetupPlayer(plr)
	local speaker = chatService:GetSpeaker(plr.Name)
	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Parent = plr
	print('why isnt this working')
	canHurt.Value = false
	local cooldown = Instance.new("NumberValue")
	cooldown.Name = "cooldown"
	cooldown.Parent = plr
	local mps = game:GetService("MarketplaceService")
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		cooldown.Value = 35
		plr:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("angry"):WaitForChild("cooldowntime").Text = "35 Second Cooldown"

	else
		cooldown.Value = 40
	end
	-- V.I.P
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		local char = plr.Character or plr.CharacterAdded:Wait()
		rs:WaitForChild("VIP"):Clone().Parent = char:WaitForChild("Head")
		speaker:SetExtraData("Tags",{{TagText = "👑V.I.P", TagColor = Color3.fromRGB(234, 255, 0)}})
		char:WaitForChild("Humanoid").WalkSpeed = 36
	end
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		game:GetService("ServerStorage"):WaitForChild("boombox"):Clone().Parent = plr:WaitForChild('Backpack')
	end
end;

-- Player Joins --

for _, plr in ipairs(ps:GetPlayers())do
	SetupPlayer(plr);
end;

ps.PlayerAdded:Connect(SetupPlayer);

Its because you are waiting for something AND requiring something. This takes time, and by the time that it is done loading that stuff the player has joined and hasn’t gotten to the PlayerAdded event.

I think the main reason it’s not working is because the speaker hasn’t been added to the channel yet. Check out the SpeakerAdded event here: In-Experience Text Chat | Documentation - Roblox Creator Hub

Maybe replace your code with this?

local rs = game:GetService("ReplicatedStorage")
local chatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
chatService.SpeakerAdded:Connect(function(speakerName)
	local speaker = chatService:GetSpeaker(speakerName)
	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Parent = plr
	print('why isnt this working')
	canHurt.Value = false
	local cooldown = Instance.new("NumberValue")
	cooldown.Name = "cooldown"
	cooldown.Parent = plr
	local mps = game:GetService("MarketplaceService")
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		cooldown.Value = 35
		plr:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("angry"):WaitForChild("cooldowntime").Text = "35 Second Cooldown"

	else
		cooldown.Value = 40
	end
	-- V.I.P
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		local char = plr.Character or plr.CharacterAdded:Wait()
		rs:WaitForChild("VIP"):Clone().Parent = char:WaitForChild("Head")
		speaker:SetExtraData("Tags",{{TagText = "👑V.I.P", TagColor = Color3.fromRGB(234, 255, 0)}})
		char:WaitForChild("Humanoid").WalkSpeed = 36
	end
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		game:GetService("ServerStorage"):WaitForChild("boombox"):Clone().Parent = plr:WaitForChild('Backpack')
	end
end)

You did forget to define plr, that would be an easy fix though.

Just have a function that is connected to the PlayerAdded event and runs for every player in the game as well:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local Players = game:GetService("Players")

local chatService = require(ServerScriptService.ChatServiceRunner.ChatService)

local function PlayerAdded(plr)
	local speaker = chatService:GetSpeaker(plr.Name)

	local canHurt = Instance.new("BoolValue")
	canHurt.Name = "canHurt"
	canHurt.Value = false
	canHurt.Parent = plr

	local cooldown = Instance.new("NumberValue")
	cooldown.Name = "cooldown"
	canHurt.Parent = plr

	local mps = game:GetService("MarketplaceService")

	if mps:UserOwnsGamePassAsync(plr.UserId, id) then
		cooldown.Value = 35
		plr.PlayerGui:WaitForChild("guistuff"):WaitForChild("angry"):WaitForChild("cooldowntime").Text = "35 Second Cooldown"
	else
		cooldown.Value = 40
	end
	-- V.I.P
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		local char = plr.Character or plr.CharacterAdded:Wait()
		rs:WaitForChild("VIP"):Clone().Parent = char:WaitForChild("Head")
		speaker:SetExtraData("Tags",{{TagText = "👑V.I.P", TagColor = Color3.fromRGB(234, 255, 0)}})
		char:WaitForChild("Humanoid").WalkSpeed = 36
	end
	if mps:UserOwnsGamePassAsync(plr.UserId,id) then
		game:GetService("ServerStorage"):WaitForChild("boombox"):Clone().Parent = plr:WaitForChild('Backpack')
	end
end

Players.PlayerAdded:Connect(PlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end