ContextActionService not working

primarily look at where i bind “SendMessage”.

local Chat = {}
Chat.__index = Chat

local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local StarterGui = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

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

local recieveMessage = require(script.recieveMessage)

function Chat.new()
	local self = setmetatable({}, Chat)
	self.UI = script.ChatUI:Clone()
	self.UI.Parent = Player.PlayerGui
	self:ListenForFriends()
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
	
	ContextActionService:BindAction("Focus", function(actionName, inputState, inputObject)
		if actionName == "Focus" and inputState == Enum.UserInputState.End then
			self.UI.Frame.InputText:CaptureFocus()
		end
	end	, false, Enum.KeyCode.Slash)
	
	self.UI.Frame.InputText.Focused:Connect(function()
		self:Focus()
	end)
	
	ReplicatedStorage.events.sendMessage.OnClientEvent:Connect(function(Player: Player, Message: string)
		self:RecieveMessage(Player, Message)
	end)
	
	return self
end

function Chat:BindEnter()
	ContextActionService:BindAction("SendMessage", function(actionName,inputState, inputObject)
	if actionName == "SendMessage" and inputState == Enum.UserInputState.Begin then
			self:SendMessage()
		end 
	end, false, Enum.KeyCode.Return)
end

function Chat:Focus(actionName, inputState, inputObject)
	self:BindEnter()
	self.UI.Frame.InputText.FocusLost:Once(function()
		ContextActionService:UnbindAction("SendMessage")
	end)
end

function Chat:SendMessage(actionName, inputState, inputObject)
	self.UI.Frame.InputText:ReleaseFocus()
	ContextActionService:UnbindAction("SendMessage")
	ReplicatedStorage.events.sendMessage:FireServer(self.UI.Frame.InputText.Text)
	self.UI.Frame.InputText.Text = ""
end

function Chat:RecieveMessage(Player: Player, Message: string)
	recieveMessage(self, Player, Message)
end

function Chat:ListenForFriends()
	Players.PlayerAdded:Connect(function(Player)
		if Player:IsFriendsWith(Players.LocalPlayer.UserId) then
			self:RecieveMessage(`Your friend {Player.DisplayName} joined the game.`)
		end
	end)
end

return Chat

it literally just wont work when i press Enter, the only time it triggers is when i unbind it, other than that it doesnt work, which is weird because the “Focus” one works, where Chat.new() is. please help and if you could explain why it doesnt work, that would be amazing

Context actions will not fire if you are focused on a text box. You should use the TextBox.FocusLost event to determine when the user presses enter

1 Like

well thats a problem… sort of. is there any way around it because what if a player unfocuses out of the roblox window, and it sends

FocusLost comes with an enterPressed variable.

Example usage:

textBox.FocusLost:Connect(function(enterPressed)
    if enterPressed then
       self:SendMessage()
    end
end)

thank you man, would not have figured that out if it wasnt for you

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