Pre-Chat V1 | Chat without typing!

9 Likes

Nahh :skull: :rofl:

tu lā€™avais dĆ©jĆ  poster sur le server de codelow en + cā€™Ć©tait un flop et mtn sur devfourm :rofl: tu tā€™es fait juste cook la @Ethanytb250

Cā€™est bien de rĆ©pondre sur des posts anglais, surtout je crois que tu es le gars sur Discord appeller El:ectrique baahhaha, surtout tu vole des freemodels

In a respectable way. If you steal someoneā€™s code and pretend that itā€™s completely yours and you made it from scratch, now thatā€™s very disrespectful and not true.

I make sure to credit people if I have taken their publicly released code and used it in many ways.

Are you sure the server script works? As I saw you changed it to server script in serverscriptservice, so localplayer wonā€™t work.

idk, i didnā€™t do anyting bro, wdymā€¦

There is a localscript in each button. This structure is bad

Your making your text message from core gui in the client, that wouldnā€™t reflect to other clients i think

  1. I donā€™t think this is allowed.
  2. Everything is client-sided, you should have tried your product before posting it.
  3. Most of the code has a lot of flaws such as:
  • No cooldown
  • Not replicating
  • The UI isnā€™t good
  • No character added updates
  • You are using multiple scripts instead of a Core script.

You are making a community resource. The source should be the first thing you should have posted.
It would be best if you learned the basics.

1 Like

image
The open button doesnt have a closing function

2 Likes

I mean this in the least harsh way, but I donā€™t think youā€™re qualified for making a Community Resource yet.
Since I was at your stage at one point, Iā€™m going to help you.
Hereā€™s what you can change to improve your Pre-Chat script:
Firstly, add a RemoteEvent to ReplicatedStorage called ā€œPrechatā€
Next, you should add a script to ServerScriptService, call it something like ā€œPrechatRecieveā€

Once you have done that, I want you to paste in this code and read the comments, really study this code:

local Remote = game.ReplicatedStorage:WaitForChild("Prechat")
local Players = game:GetService("Players")
local CooldownDuration = .75 -- How long of a cooldown you want between each message a player can send.
local Cooldown = {}
local AllowedMessages = {
	-- We associate a message with an ID so exploiters cannot use non-existing messages and send obscene messages
	[1] = "Hello!",
	[2] = "Yes.",
	[3] = "No.",
	[4] = "How are you?",
	[5] = "Good.",
	[6] = "Bad.",
	[7] = "Okay.",
	[8] = "Got it.",
	[9] = "I am planting the C4 at Bombsite B."
	-- You can add more messages to the list but make sure to increment the number.
}

Players.PlayerAdded:Connect(function(Player)
	-- Add a new player object to the cooldown list
	Cooldown[Player.UserId] = tick()
	-- Tick gets the current second with decimal place
	-- We get the ID of the player and add it to the Cooldown list
end)

Players.PlayerRemoving:Connect(function(Player)
	Cooldown[Player] = nil -- Removes the player from the cooldown list so we don't encounter a memory leak
	-- A memory leak is when more data is exponentially used and is not disposed.
end)

Remote.OnServerEvent:Connect(function(Player, ID)
	local Message = AllowedMessages[ID]
	if Message then -- This checks whether the message is real and existing
		local LastMessage = Cooldown[Player.UserId] -- Gets the last time the player has sent a message.
		
		if tick()-LastMessage>CooldownDuration then
			-- Great! We can now send the message to all clients.
			print("Recieved and sending.") -- You can remove this print statement if you wish.
			Remote:FireAllClients(Player, Message)
			Cooldown[Player.UserId] = tick() -- Resets the timer
		else
			Remote:FireClient(Player, "Sorry! You are on cooldown for sending Prechat messages.")
			-- Informs the player that they are on cooldown.
		end
	end
end)

Great, we can now receive messages as the server and verify them and send them back to the client, this is going good so far.

Working screenshot:
image
Error below is unrelated and from a plugin

Now we have to setup a client, which is easier said than done, but Iā€™ll do that hard work for you and all you have to do is study the code and read the comments. Call the LocalScript ā€œPrechatClientā€

local Remote = game.ReplicatedStorage:WaitForChild("Prechat")
local Chat = game:GetService("Chat")

Remote.OnClientEvent:Connect(function(Sender:Player,Message)
	-- This fires when the server sends the message to the client.
	if Sender and Sender.Character then
		-- Checks that the sender has a character
		local Head = Sender.Character:FindFirstChild("Head")
		if Head then
			Chat:Chat(Head,Message) -- Displays the message
			-- You can use DisplaySystemMessage but that isn't working for me right now, so you will have to do that on your own.
		end
	end
end)

Add the client script to StarterPlayerScripts and voila, we have the main handler.
Letā€™s see what it looks like when we send the message using the Command Line
image
ā€¦
loud silence
Wait, we need the UI to properly send the messages!
Letā€™s set up a basic message sending UI.
It should follow this order in Explorer:
image
The buttons are named based on their message ID, which is in the PrechatRecieve script, in ServerScriptService. This is not the best way to do this, but itā€™ll work for the sake of this walk-through. You also do not need a UIGridLayout, but Iā€™m doing it so I can be lazy and not do the UI.

Add a LocalScript to the parent of buttons, which in my case is a frame.
Name the LocalScript something along the lines of Messaging, as it will serve the purpose of messaging the server.

Yet again, I want you to study this code and read ALL of the comments in detail.

local Remote = game.ReplicatedStorage:WaitForChild("Prechat")

wait(1) -- Waits for all the UI to load

for _,v in pairs(script.Parent:GetChildren()) do
	-- Loops through all of the children of the frame
	if v:IsA("TextButton") then
		-- Checks whether the child is a button, so it doesn't detect the script or the layout
		v.MouseButton1Click:Connect(function()
			-- Fires whenever it gets clicked.
			Remote:FireServer(tonumber(v.Name)) -- Sends a message to the server.
		end)
	end
end

Works like a charm!

I have updated it and fixed the issue where you could only send one message.

4 Likes

I have now put this into a bundle which you can import by pressing the Model button:
PrechatFixedBundle.rbxm (10.3 KB)
image

2 Likes