I want to check if a player has voice chat when they join, then print out either “success” if they have voice chat, or “fail” if they don’t have voice chat.
This is what I have so far:
local Players = game:GetService("Players")
local VoiceChatService = game:GetService("VoiceChatService")
local LocalPlayer = Players.LocalPlayer
game.Players.PlayerAdded:Connect(function()
if VoiceChatService:IsVoiceEnabledForUserIdAsync(LocalPlayer.UserId) then
print("success")
else
print("fail")
end
end)
I don’t have voice chat, but I should see “Fail” in the output. I don’t.
Someone please tell me what I did wrong and how to fix it!
I noticed you are accessing LocalPlayer, so I will assume this script has Client RunContext. If you are only single-player testing, you will have joined before your code begins to listen for PlayerAdded.
local Players = game:GetService("Players")
local VoiceChatService = game:GetService("VoiceChatService")
local LocalPlayer = Players.LocalPlayer
if VoiceChatService:IsVoiceEnabledForUserIdAsync(LocalPlayer.UserId) then
print("Has voice chat enabled.")
else
print("Doesn't have voice chat enabled.")
end
Should this be in a regular script? It is in a local script, there was no error or output. I changed it to a regular script, and it says attempt to nil index with UserId.
game.Players.PlayerAdded:Connect(function(plr)
if game:GetService("VoiceChatService"):IsVoiceEnabledForUserIdAsync(plr.UserId) then
print("Got VC")
else
print("No VC")
end
end)