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