Microphone tool

So I’m working with a Dev team to make a Hotel group and I came across the idea of a handheld microphone to enable the host of a training to speak with a message. I have made the script for the microphone to work in Workspace, but I am struggling to make the microphone work in StarterPack (as a Tool).

StarterPack layout: image

Script:

function onChatted(msg, recipient, speaker) 

local source = string.lower(speaker.Name)
msg = string.lower(msg)
local pos1 = script.Parent.Position
local pos2 = speaker.Character.Head.Position
dist=(pos1-pos2).magnitude
dist=dist-math.max(math.max(speaker.Character.Head.Size.x, speaker.Character.Head.Size.z), speaker.Character.Head.Size.y)
if (dist <= 5 and script.Parent.On.Value == true) then
local m = Instance.new("Message")
m.Parent = game.Workspace
m.Text = msg
wait(3)
m:remove()
end

end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered)

As I say, it works fine in Workspace but I can’t make it work as a Tool.
Any help would be great.

1 Like

Have you tried looking into the Developer’s Hub tutorial on tool creations? You can find it here: In-Experience Tools | Documentation - Roblox Creator Hub

1 Like

I have but it was no use to me. I think the issue is that the microphone itself isn’t working.

Each time a character is added all tools from the starter pack are copied to the backpack. Scripts in starterpack even if their property Disabled is false, they are disabled so they won’t run. That is why your event listener

game.Players.ChildAdded:connect(onPlayerEntered)

doesn’t listen to the event being fired before the tool is copied to the Player’s backpack.
That is why, to make it work we have to add that at the end of your code:

for _,plr in pairs(game.Players:GetPlayers()) do
    plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end