Chat detector working for all players (text chat service)

I have a spell script and it’s only supposed to start the spell for the person who says it, but it starts it for EVERY player in game when only one person says it.

script: (local script in startercharacterscripts)

local TextChatService = game:GetService("TextChatService")
local player = game:GetService("Players").LocalPlayer
local abilityName = "Explosion"
local UIS = game:GetService("UserInputService")
local cooldown = 40
local onCooldown = false
local Mouse = player:GetMouse()
local Character = player.Character
local event = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Spells")

local loadedSpell = player:WaitForChild("LoadedSpell")

local spellReady = false

local debounce = false

local Notifications  = require(game.ReplicatedStorage.BannerNotification_Storage.BannerNotificationModule)
local notifConfig = {
	.3, 							-- Background Transparency
	Color3.fromRGB(0, 0, 0), 		-- Background Color

	0, 								-- Content Transparency
	Color3.fromRGB(255, 255, 255), -- Content Color
}

TextChatService.MessageReceived:Connect(function(message)
	local content = message.Text:lower()
	local targetPhrase = "despulso"
	

	if content:match(targetPhrase) and player.Character.Humanoid.Values.CanUseMagic.Value and not player.Character.Ragdoll.Value and player.Character.Humanoid.Health > 0 then
		if player:GetRankInGroup(11954854) >= 3 then
			if player:FindFirstChild("Cooldowns"):FindFirstChild("ExplosionCooldown") then
				--			print("Spell is still on cooldown, ignoring input")
				return
			end
			--		print("Spell recognized: Despulso")
			loadedSpell.Value = "Despulso"
		else
			Notifications:Notify("Pyrokinesis", "You need to be a Blooming Practitioner or above to use Explosion.", "rbxassetid://11419713314", 5.5, notifConfig)
			loadedSpell.Value = "None"
		end
	end
end)

UIS.InputBegan:Connect(function(input, GPE)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if loadedSpell.Value == "Despulso" and player.Character.Humanoid.Values.CanUseMagic.Value and not player.Character.Ragdoll.Value  and player.Character.Humanoid.Health > 0 then
			if not player:FindFirstChild("Cooldowns"):FindFirstChild("ExplosionCooldown") then
				loadedSpell.Value = "None"
				event:FireServer("Explosion", nil, Mouse.Hit.Position)
				game.ReplicatedStorage.Events.CooldownManager:FireServer("Start", abilityName, cooldown)
			else
				Notifications:Notify("Cooldown", "Explosion is on cooldown for "..player.Cooldowns.ExplosionCooldown.Value.." seconds.", "rbxassetid://7634887649", 5, notifConfig, player)
				--			print("Spell is still on cooldown, ignoring input")
				loadedSpell.Value = "None"
				return
			end
		elseif player.Character.Humanoid.Values.CanUseMagic.Value == false then
			Notifications:Notify("Abilities Disabled", "Your abilities are disabled.", "rbxassetid://11419713314", 5, notifConfig, player)
			loadedSpell.Value = "None"
			return
		end
	end
end)

I’m assuming it’s because message received is invoked whenever you get a message, even if it isn’t yours. So you should check if the player who sent it was the local player. Or use Player.Chatted

Why don’t you use TextChatCommands? They are practical and perfect for this, since when a command is triggered the server does not show the message in the chat, it just executes the function of that command.

1 Like

player.chatted doesn’t work with text chat service, thats the whole reason im using this method to detect chats

and for some reason roblox decided to make it so you can’t see who sent the message, so theres no way to check if they’re the local player (at least from what ive read on the posts about text chat service)

I haven’t found a way to add my own text chat commands, ive read the docs a few times but I just keep seeing the roblox integrated ones

This triggers when anyone sends a message. You can access the message data to determine the sender and run the rest of the code accordingly.

local playerId = message.TextSource.UserId

You can then do ”if game.Players.LocalPlayer.UserId == playerId then” for example.

You can get the server to verify who the sender is I modified your script with an additional server script.

local script:

local TextChatService = game:GetService("TextChatService")
local player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local event = ReplicatedStorage:WaitForChild("Events"):WaitForChild("Spells")
local spellRequestEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("SpellRequest")

local abilityName = "Explosion"
local cooldown = 40
local Mouse = player:GetMouse()
local loadedSpell = player:WaitForChild("LoadedSpell")

local debounce = false
local Notifications  = require(game.ReplicatedStorage.BannerNotification_Storage.BannerNotificationModule)
local notifConfig = {
	.3, 							-- Background Transparency
	Color3.fromRGB(0, 0, 0), 		-- Background Color

	0, 								-- Content Transparency
	Color3.fromRGB(255, 255, 255), -- Content Color
}

local targetPhrase = "despulso"

TextChatService.MessageReceived:Connect(function(message)
	local content = message.Text:lower()

	spellRequestEvent:FireServer(content)
end)

UIS.InputBegan:Connect(function(input, GPE)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		if loadedSpell.Value == "Despulso" and player.Character and player.Character:FindFirstChild("Humanoid") then
			local humanoid = player.Character:FindFirstChild("Humanoid")
			local canUseMagic = humanoid:FindFirstChild("Values") and humanoid.Values:FindFirstChild("CanUseMagic")

			if canUseMagic and canUseMagic.Value then
				if not player:FindFirstChild("Cooldowns"):FindFirstChild("ExplosionCooldown") then
					loadedSpell.Value = "None"
					event:FireServer("Explosion", nil, Mouse.Hit.Position)
					game.ReplicatedStorage.Events.CooldownManager:FireServer("Start", abilityName, cooldown)
				else
					Notifications:Notify("Cooldown", "Explosion is on cooldown for "..player.Cooldowns.ExplosionCooldown.Value.." seconds.", "rbxassetid://7634887649", 5, notifConfig, player)
					loadedSpell.Value = "None"
				end
			else
				Notifications:Notify("Abilities Disabled", "Your abilities are disabled.", "rbxassetid://11419713314", 5, notifConfig, player)
				loadedSpell.Value = "None"
			end
		end
	end
end)

Server Script:

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

local spellRequestEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("SpellRequest")

spellRequestEvent.OnServerEvent:Connect(function(player, content)
	local targetPhrase = "despulso"

	if content:match(targetPhrase) then
		local character = player.Character
		if character and character:FindFirstChild("Humanoid") and character:FindFirstChild("Humanoid").Health > 0 then
			local humanoid = character:FindFirstChild("Humanoid")
			local canUseMagic = humanoid:FindFirstChild("Values") and humanoid.Values:FindFirstChild("CanUseMagic")
			local ragdoll = character:FindFirstChild("Ragdoll")

			if canUseMagic and canUseMagic.Value and not ragdoll.Value then
				if player:GetRankInGroup(11954854) >= 3 then
					local cooldowns = player:FindFirstChild("Cooldowns")
					if cooldowns and not cooldowns:FindFirstChild("ExplosionCooldown") then
						local loadedSpell = player:FindFirstChild("LoadedSpell")
						if loadedSpell then
							loadedSpell.Value = "Despulso"
						end
					end
				else
					local Notifications  = require(ReplicatedStorage.BannerNotification_Storage.BannerNotificationModule)
					local notifConfig = {
						.3, Color3.fromRGB(0, 0, 0), 0, Color3.fromRGB(255, 255, 255)
					}
					Notifications:Notify("Pyrokinesis", "You need to be a Blooming Practitioner or above to use Explosion.", "rbxassetid://11419713314", 5.5, notifConfig)
				end
			end
		end
	end
end)