Script doesn't work with no errors

I. What do you want to achieve?
Custom “:setmessage” command.
II. What is the issue?
I don’t know, script isn’t working with no errors.
III. What solutions have you tried so far?
Tried to do StarterGui instead of PlayerGui but failed.

Code:

-- [ STARTING VARIABLES ] --

local commands = {}

local prefix = ":" 

-- [ MAIN CODE ] --

-- Setmessage Command --

commands.setmessage = function(sender, args) -- sender: Object - args : Table
	-- What will happen when you run the command.
	print("Setmessage command ran by:")
	print(sender)
	
	for i, playerName in pairs(args) do
		print(playerName)
	end
	
	local MessageToSet = args[1]
	
	game.Players.PlayerAdded:Connect(function(plr)
 
	local plrG = plr:WaitForChild("PlayerGui")
	local gui = plrG:WaitForChild("ScreenGui")
	local frame = gui:WaitForChild("MainFrame")
	local msgbox = frame:WaitForChild("msg")	
		
	msgbox.Text = (MessageToSet)
end)
 
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		if player.Name == "airsoft561" then
		message = string.lower(message) -- TP = tp / Tp = tp etc.
		
		local splitString = message:split(" ") -- {":setmessage","message"}
		
		local prefixCmd = splitString[1] -- ":setmessage"
		
		local cmd = prefixCmd:split(prefix) -- {":","setmessage"} -- another table
		
		local cmdName = cmd[2]
		
		
		if commands[cmdName] then
			
			local args = {} -- Other splited, eg: "yeet" if you did: ":setmessage yeet"
			
			for i = 2, #splitString, 1 do
				table.insert(args,splitString[i])
			end
			
			commands[cmdName](player,args) -- Fires function
				
			end
		else
			print("Player not authorized.")
		end
		-- ":setmessage Interviews" ----> {":setmessage","Interviews"} table
	end)
end)

[EDIT i]
I attached an image of the location of the script and the gui.
[EDIT ii]
I wrote "if player.Name == “airsoft561"” to test a criteria and to test if it only runs if the criteria is met.
[EDIT iii]
“titl” stands for “title” (textlabel in gui)

[ATTACHMENT]
image

You should use RemoteEvents. Your script won’t work since its setting the text everytime a player joins. Plus, manipulating UI’s on the Server is Bad Practice and should be avoided.

I can’t use remoteevents because how can I fire an event that sets the text of a TextLabel that has the arguments I want it to set it in another script.

MMM. you are wrong. u can do this Remote:FireAllClients(message) and on the other end do this: Remote.OnServerEvent:Connect(function(message)

Still doesn’t work.

MAIN CODE (Script in ServerScriptService, “CustomAdminCmds”)

-- [ STARTING VARIABLES ] --

local commands = {}

local prefix = ":" 

local RS = game:GetService("ReplicatedStorage")

local Remote = RS:WaitForChild("RemoteEvent")

-- [ MAIN CODE ] --

-- Setmessage Command --

commands.setmessage = function(sender, args) -- sender: Object - args : Table
	-- What will happen when you run the command.
	print("Setmessage command ran by:")
	print(sender)
	
	for i, playerName in pairs(args) do
		print(playerName)
	end
	
	local MessageToSet = args[1]
	
	Remote:FireAllClients(MessageToSet)
 
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		if player.Name == "airsoft561" then
		message = string.lower(message) -- TP = tp / Tp = tp etc.
		
		local splitString = message:split(" ") -- {":setmessage","message"}
		
		local prefixCmd = splitString[1] -- ":setmessage"
		
		local cmd = prefixCmd:split(prefix) -- {":","setmessage"} -- another table
		
		local cmdName = cmd[2]
		
		
		if commands[cmdName] then
			
			local args = {} -- Other splited, eg: "yeet" if you did: ":setmessage yeet"
			
			for i = 2, #splitString, 1 do
				table.insert(args,splitString[i])
			end
			
			commands[cmdName](player,args) -- Fires function
				
			end
		else
			print("Player not authorized.")
		end
		-- ":setmessage Interviews" ----> {":setmessage","Interviews"} table
	end)
end)

UI CODE (LocalScript inside of the Gui)

-- [ STARTING VARIABLES ] --

local RS = game:GetService("ReplicatedStorage")

local Remote = RS:WaitForChild("RemoteEvent")

-- [ MAIN CODE ] --

Remote.OnServerEvent:Connect(function(MessageToSet)

script.Parent.MainFrame.msg.Text = (MessageToSet)

end)

[Attachment]
image

Try adding some prints troughout the code.

EDIT: its Remote.OnClientEvent

It works, also, can EVERYONE in the server see this? Or only the player who runs it see it?

Everyone. Because its a RemoteEvent and you are using FireAllClients()

Just a heads up, you should also be filtering the message as well

1 Like