Player.chatted command not working

Trying to make a text command that hides/shows city labels. Why doesn’t this work?

local player = game.Players.LocalPlayer
local CityGUI = game.ReplicatedStorage:WaitForChild("CityGUIs")
local cooldown = false


local function updateGuiVisibility()
	cooldown = true
	if CityGUI.Parent == workspace then
		CityGUI.Parent = game.ReplicatedStorage
		game.StarterGui:SetCore("SendNotification", {
			Title = "✅ Labels Hidden",
			Text = "You can always say /labels to make them visible again.",
			Duration = 3
		})
	else
		CityGUI.Parent = workspace
		game.StarterGui:SetCore("SendNotification", {
			Title = "✅ Labels Visible",
			Text = "You can always say /labels to hide them.",
			Duration = 3
		})
	end
		cooldown = false
end


player.CharacterAdded:Connect(function()
	player.Chatted:Connect(function(message)
		if message:lower() == "/labels" then
			if cooldown then
				game.StarterGui:SetCore("SendNotification", {
					Title = "❌ Cooldown",
					Text = "Please wait a few seconds before toggling labels again.",
					Duration = 2
				})
			else
				updateGuiVisibility()
			end
		end
	end)
end)

If you are using a custom chat, this could jeopardize the .Chatted function, to fix this either reconnect it properly to .Chatted or make a remote event that sends messages over.

You’re connecting Player.Chatted in Player.CharacterAdded.

I don’t know why you’re connecting it when the character is added in the first place, but your code is probably running after the character has already been added, meaning it’ll only get connected when respawning (and stacking each time).

2 Likes

It seems strange to have the GUI switch its parent object between the Workspace and ReplicatedStorage. I would suggest keeping the GUI in the Workspace and simply toggle its enabled property.

local CoolDownDuration = 3




local Player = game.Players.LocalPlayer
local CityGUI = game.Workspace:WaitForChild("CityGUIs")
local LastRan = game.Workspace.DistributedGameTime

local function IsCoolDownActive()
	if (game.Workspace.DistributedGameTime - LastRan) < CoolDownDuration then
		return true
	end
	LastRan = game.Workspace.DistributedGameTime
	return false
end

local function UpdateGuiVisibility()
	if CityGUI.Enabled then
		CityGUI.Enabled = false
		game.StarterGui:SetCore("SendNotification", {
			Title = "✅ Labels Hidden",
			Text = "You can always say /labels to make them visible again.",
			Duration = 3
		})
	elseif not CityGUI.Enabled then
		CityGUI.Enabled = true
		game.StarterGui:SetCore("SendNotification", {
			Title = "✅ Labels Visible",
			Text = "You can always say /labels to hide them.",
			Duration = 3
		})
	end
end


Player.Chatted:Connect(function(Message)
		if Message:lower() == "/labels" then
			if IsCoolDownActive() then
				game.StarterGui:SetCore("SendNotification", {
					Title = "❌ Cooldown",
					Text = "Please wait a few seconds before toggling labels again.",
					Duration = 2
				})
			else
				UpdateGuiVisibility()
			end
		end
end)

I removed that part but it still isn’t working unfortunately

Do you mind sending over a place file so I can take a closer look?

So the issue is that CityGUI is a folder, that holds about 50 billboard guis (all names of cities) adorneed to parts in the workspace. I tried changing the properties at first but Roblox limits got in the way, and adding wait() makes it look messy

What limits are you encountering?
Last I checked there were no rate limits to datamodel operations.

I had it changing the visibility of each gui, but it would only do about half of them then stop.

I can put the script and cities in a separate place give me one moment

Do you have StreamingEnabled? They might not exist on the client when you’re running the code.

Yes I do. I’m not really sure what the difference is though having it off or on

Instance streaming requires your code to be built around it, parts in workspace might not be loaded when your code runs, and you need to build you code to wait for it, or run when it loads.

My code does wait for the parts. I was using this same code but using “L” key input to toggle them, and it worked just fine. It’s just the text command that isn’t working now. I wanted to make something that could be used by mobile players also.

Here is the place:

ToggleLabelsTesting.rbxl (65.9 KB)

Ah wait I see what the issue is.
Player.Chatted doesn’t fire on clients (or at least, no longer does.), you’ll need to use TextChatService.SendingMessage.

1 Like