Clicking GUI to sent a message in the chat

  1. What do you want to achieve?
    I want to have a function to when you click on a GUI button, it will actually sent a message in the chat. I have better uses for this then just that.

  2. What is the issue?


    2 things to note here:


-1: when you join the game it sent’s a message already.

2:clicking on the button doesn’t do anything


  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried here and other places but, no-one was talking on how to do a click ChatMakeSystemMessage.

It shows no errors or anything like that as everything works fine, it’s just getting to to work properly is the issue. also not sure which script it could be likely that isn’t working properly so here’s 3 of them

=====================================

local TestEvent = game.ReplicatedStorage:WaitForChild("ClickedUI")

game.ReplicatedStorage.PlayerJoined.OnClientEvent:Connect(function(PlrName)
	
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: "..PlrName.." has joined the game",
		Color = Color3.fromRGB(75, 151, 75),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})
end)

game.ReplicatedStorage.PlayerLeft.OnClientEvent:Connect(function(PlrName)

	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: "..PlrName.." has left the game",
		Color = Color3.fromRGB(196, 40, 28),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})
end)

TestEvent.OnClientEvent:Connect(function(PlrName)
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: "..PlrName.." said hi to all of you :)",
		Color = Color3.fromRGB(255, 255, 65),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})
end)

=====================================

local RS = game.ReplicatedStorage:WaitForChild("ClickedUI")

game.Players.PlayerAdded:Connect(function(Player)
	
	game.ReplicatedStorage.PlayerJoined:FireAllClients(Player.Name)
end)

game.Players.PlayerRemoving:Connect(function(Player)

	game.ReplicatedStorage.PlayerLeft:FireAllClients(Player.Name)
end)

game.Players.PlayerAdded:Connect(function(Player)
	
	RS:FireAllClients(Player.Name)
end)

=====================================

local RS = game.ReplicatedStorage:WaitForChild("ClickedUI")

local function Clicked(Player)
	RS.OnClientEvent:Connect(Player)
end

script.Parent.MouseButton1Click:Connect(Clicked)

=====================================
do not write entire scripts or design entire systems.

when this event fires it sends the message saying that X said hi (when they join), not when the player clicked it

well is there any way to do it in a click function then?

because I don’t want to do it when you join in the game, just want it so when you click on the GUI button it sent’s a message if there’s a different/better way around it.

local function Clicked(Player)
	RS.OnClientEvent:Connect(Player)
end

You’re not sending the message here, it’s listening for the remote which shouldn’t be what it’s doing.

Instead, do this.

local function Clicked(Player)
	RS.FireClient(Player)
end

Then remove the script that @Mister33j had pointed out, since you don’t need to be using it when they joined. PlayerAdded, is to detect when a Player Joined. Not for when the player clicked on a GUI.

You can fire it by either using FireClient (executes for only the player) or FireAllClients (executes for every player within the experience).

ok yeah that fixes it however, when I click on the Button. It gives out an error saying (Attempt to connect failed: Passed value is not a function)

I did remember testing around with the script which did work fine, and then broken something to it. and never came back for a few months trying to fix this issue until now.

the error comes from this function:


local RS = game.ReplicatedStorage:WaitForChild(“ClickedUI”)

local function Clicked(Player)

RS.OnClientEvent:Connect(Player)

end

script.Parent.MouseButton1Click:Connect(Clicked)

Player is being used as “Mouse”, because it’s one of MouseButton1Click’s values. Remove the Player thats being used for Clicked and instead make a variable to get Player. I’m assuming this is being done with a LocalScript so.

local RS = game.ReplicatedStorage:WaitForChild(“ClickedUI”)

local Player = game.Players.LocalPlayer

local function Clicked()

RS:FireClient(Player)

end

script.Parent.MouseButton1Click:Connect(Clicked)

yes it is using a localscript for this, also am so close to getting this to work and thanks for the help :slight_smile:

anyways gonna try to see if it works or not.

same error message again and this time it’s pointing toward the ( RS.OnClientEvent:Connect(Player) ) function.

I’ve edited the code. If any more error occurs let me know.

It’s still giving out errors on the ( RS:FireClient(Player) ) however, I have tried the other ones like (FireAllClient) or (FireServer) and tho didn’t work or gave errors.

it gives this error: (FireClient can only be called from the server)

TestEvent.OnServerEvent:Connect(function(PlrName)
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: "..PlrName.." said hi to all of you :)",
		Color = Color3.fromRGB(255, 255, 65),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})

The script that the playeradded and playerremoved is in

yeah seems like it might have to do with that maybe

That’s the changed edition. Here, I’ll make changes with the entire script and send it here.

local TestEvent = game.ReplicatedStorage:WaitForChild("ClickedUI")
Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: " .. Player.Name .. " has joined the game",
		Color = Color3.fromRGB(75, 151, 75),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})
end)

Players.PlayerRemoved.OnClientEvent:Connect(function(Player) -- For PlayerJoined and PlayerLeft, you don't need those as roblox already has an event for that located in (game.Players).

	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: " .. Player.Name .. " has left the game",
		Color = Color3.fromRGB(196, 40, 28),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})
end)

TestEvent.OnServerEvent:Connect(function(Player) -- Changed from OnClientEvent to OnServerEvent. Since the button is being fired from a local script, it can only be received for the server in a normal script.

	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[SYSTEM]: " .. Player.Name .. " said hi to all of you :)",
		Color = Color3.fromRGB(255, 255, 65),
		Font = Enum.Font.FredokaOne,
		TextSize = 18,
	})
end)

the only error it gives on the new script you posted is (PlayerRemoved is not a valid member of Players “Players” ), it also broken the join message as it doesn’t appear no-more.

and I think something is wrong with the other one ( RS:FireClient(Player) ). or it could be that (PlayerRemoved) error causing it to not work.

Change PlayerRemoved To PlayerRemoving.

changing that now gives an error of saying (OnClientEvent is not a valid member of RBXScriptSignal)

jeezs this is gonna take a bit to find a way to get this working, thought it would have been simple and straight forward and that would have been it. guess not tho lol.

just to show prove and that this isn’t a waste of time here.

here’s a screenshot of the output

where is this script located? why is it showing an error on the “RBXScriptsignal?”

the error is showing on this script

it’s in the text button in a local script


local RS = game.ReplicatedStorage:WaitForChild(“ClickedUI”)

local Player = game.Players.LocalPlayer

local function Clicked()

RS.OnClientEvent(Player)

end

script.Parent.MouseButton1Click:Connect(Clicked)


Once again that line should be RS:FireClient(Player)