Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to fix this Microphone Message script?

Asked by 10 years ago

I have a working Microphone, that of course works when spoken into from a certain distance. What I would like is before the message is say "Message from (Speakers Name Here)" But i cant seem to get it working? Anyone able to diagnose this problem?

function onChatted(msg, recipient, speaker) 

local source = string.lower(speaker.Name)
msg =(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 = "Message from (speaker.Name): (msg)"
wait(5)
m:remove()
end

end 

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

game.Players.ChildAdded:connect(onPlayerEntered)

From this all I get is, "Message from (speaker.Name): (msg)", which isn't what I want. How do I have the variables actually be replaced by what is stated?

0
ALSO: Use the PlayerAdded event, not ChildAdded. Perci1 4988 — 10y

1 answer

Log in to vote
5
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
10 years ago

You improperly concatenated the string values in the line where you changed your message. In order to combine a string with variables (that are also strings) you need to use two periods (..) to separate them as seen below in my example:

m.Text = "Message from " .. speaker.Name .. ": " .. msg
0
Thank you very much for your assistance! +1~ FaultyMatrix 30 — 10y
Ad

Answer this question