Script doesn't work, but I don't get where is the problem

I was trying to create a radio system, and I built it like this:

  • I created a test part, which should be the radio, and put it inside an accessory. Inside the part there is a localscript called “RadioScript” (and put it in ServerStorage because, with an hat giver, the radio clone and parent it to character);

image

  • I also created a ScreenGUI in the StarterGUI, with a frame inside with BackgroundColor3 (255, 0, 0) and a localscript.

image

Returning to RadioScript, the script works that if the BackgroundColor3 of the frame is (0, 255, 0) (green), when the player writes something, a part renamed “TestRadioPart” will display what the player said (via the “Chat” service). The script is as follows:


local player = game.Players.LocalPlayer

local playerGui = player:WaitForChild("PlayerGui")
local radioGui = playerGui:WaitForChild("Radio")
local frame = radioGui:WaitForChild("Frame")

local lastMessage = nil

local testUscitaRadio = workspace.TestRadioPart

game.TextChatService.OnMessageAdded = function(msg)
	if frame.BackgroundColor3 == Color3.fromRGB(0, 255, 0) and msg.Status == Enum.TextChatMessageStatus.Success then
		game:GetService("Chat"):Chat(testUscitaRadio, msg.Text, Enum.ChatColor.Red)
		lastMessage = msg.Text
	end
end

Everything works, however, when the player who had the radio dies, he can no longer write in chat and output a warn:

Error occurred while calling TextChatService.OnIncomingMessage: Script that implemented this callback has been destroyed while calling async callback

I read another forum with the same problem, and I tried using “OnMessageAdded” instead of “OnIncomingMessage”. However, the output gives an error:

OnMessageAdded is not a valid member of TextChatService “TextChatService”

Then I tried using CharacterAdded, but it only gets worse since the TestRadioPart no longer works.

I no longer know what to try to make it work, which is why I want to ask someone more experienced for help who can help me.

P.S. I would like to point out that I am a beginner, so I apologize in advance if the organization of the system is not one of the best😭

P.P.S. Idk if this can help, i don’t think, but there is the localscript of the GUI:


local frame = script.Parent.Frame

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

local radio = plr.Character:WaitForChild("Radio")

game:GetService("UserInputService").InputBegan:Connect(function(key, gPE)
	if radio.Parent == plr.Character then
		if gPE then return end
		
		if key.KeyCode == Enum.KeyCode.T then
			if frame.BackgroundColor3 == Color3.fromRGB(255, 0, 0) then
				frame.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
			elseif frame.BackgroundColor3 == Color3.fromRGB(0, 255, 0) then
				frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
			end
		end
	end
end)

local hum = plr.Character:WaitForChild("Humanoid")

hum.Died:Connect(function()
	frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end)

What is gPE it seems like it isn’t defined

It’s gameProcessedEvent, to avoid that when I write in chat the GUI changes color.

It isn’t defined maybe it not use it

Wdym with “It isn’t defined”? I need it bcz if I type in chat the letter “T” the GUI changes color, and I don’t want that.

Like a variable for game process event

Here’s a possible solution:

local TextChatService = game:GetService('TextChatService')
local ChatService = game:GetService('Chat')
local Players = game:GetService('Players')

local testOutputRadio = workspace:WaitForChild('TestRadioPart')

TextChatService.SendingMessage:Connect(function(msg)
    local player = Players:GetPlayerByUserId(msg.TextSource.UserId)
    if not player then
        warn('Player not found')
        return
    end

    local playerGui = player:WaitForChild('PlayerGui')
    local radioGui = playerGui:FindFirstChild('Radio')

    if radioGui then
        local frame = radioGui:FindFirstChild('Frame')
        if frame then
            if frame.BackgroundColor3 == Color3.fromRGB(0, 255, 0) and msg.Status == Enum.TextChatMessageStatus.Sending then
                ChatService:Chat(testOutputRadio, msg.Text)
            end
        end
    end
end)

By reviewing the LocalScript that manages the Radio, I noticed that you are using the OnMessageAdded event, which does not exist for the TextChatService instance.

To ensure that the event is triggered, you need to use the SendingMessage event.

Then, within the event, you need to create the player variable, which can be found using the Players:GetPlayerByUserId function.

Once the player is assigned, you need to verify that the RadioGui has been assigned to the player’s PlayerGui who sent the message, and finally, you can verify the status of the Frame and the status of the Message.

Be careful with one thing! The Message status is not Success but Sending, as we are verifying it when the player is sending the message.

Fix RadioGUI LocalScript

Reviewing the LocalScript for the radio, I didn’t notice any errors. However, I’ve created a safer version for you to prevent any errors or infinite yields during execution.

local UserInputService = game:GetService('UserInputService')
local player = game:GetService('Players').LocalPlayer

local GUI = script.Parent -- Move the LocalScript in the GUI
local frame = GUI:WaitForChild("Frame")

local char = player.Character or player.CharacterAdded:Wait()
local radio = char:WaitForChild("Radio")

local hum = char:WaitForChild('Humanoid')

hum.Died:Connect(function()
	frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
end)

UserInputService.InputBegan:Connect(function(key, gPE)

	if not radio or not char:WaitForChild("Radio") then return end
	if gPE then return end

	if key.KeyCode == Enum.KeyCode.T then
		if frame.BackgroundColor3 == Color3.fromRGB(255, 0, 0) then
			frame.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
		elseif frame.BackgroundColor3 == Color3.fromRGB(0, 255, 0) then
			frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
		end
	end
end)

I hope you have found a solution with my help :pray:

2 Likes

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