So, i am making a voice chat script and testing out the ban feature.
It is supposed to check if the speaker is an admin, so only admins can use the ban feature.
It just returns: 77, attempt to index local 'speaker' (a nil value)
I tried copying and pasting from wiki but the error still repeats!
Anyways, enough of this talking. Here is the code:
-- get banned and admins local speakbanned = {} local admins = {ARTEK22} -- get sounds local model = Instance.new("Model") model.Name = "Chat Voice sounds" model.Parent = workspace local trains = Instance.new("Sound") -- make a new sound trains.Parent = model trains.SoundId = "rbxassetid://165384079" -- set id trains.Looped = false -- dont loop local no = Instance.new("Sound") no.Parent = model no.SoundId = "rbxassetid://151908665" no.Looped = false local only = Instance.new("Sound") only.Parent = model only.SoundId = "rbxassetid://132149993" only.Looped = false local scarce = Instance.new("Sound") scarce.Parent = model scarce.SoundId = "rbxassetid://528207007" scarce.Looped = false for i,v in pairs(game.Players:GetChildren()) do -- when somebody joins local sgui = Instance.new("ScreenGui") -- gui making procces sgui.Parent = v.PlayerGui local frame = Instance.new("Frame") frame.Parent = sgui frame.BackgroundColor3 = BrickColor.Black().Color frame.Position = UDim2.new(0, 200, 0, 25) frame.Size = UDim2.new(0, 300, 0, 300) local title = Instance.new("TextLabel") title.BackgroundTransparency = 1 title.TextStrokeTransparency = 0 title.Parent = frame title.TextScaled = true title.TextColor3 = BrickColor.White().Color title.Text = "Chat Voice" title.Size = UDim2.new(0, 300, 0, 50) local info = Instance.new("TextLabel") info.BackgroundTransparency = 1 info.TextStrokeTransparency = 0 info.Parent = frame info.TextScaled = true info.Position = UDim2.new(0, 000, 0, 75) info.TextColor3 = BrickColor.White().Color info.Text = 'Type in "voicechat/commands" to see the commands!' info.Size = UDim2.new(0, 300, 0, 50) local textButton = Instance.new("TextButton") textButton.BackgroundColor3 = BrickColor.Red().Color textButton.TextColor3 = BrickColor.White().Color textButton.Parent = frame textButton.Position = UDim2.new(0, 100, 0, 200) textButton.Size = UDim2.new(0, 100, 0, 50) textButton.TextScaled = true textButton.Text = "Got it!" textButton.MouseButton1Down:Connect(function() frame:Destroy() end) v.Chatted:connect(function(msg, speaker) -- when somebody says something print(speaker.Name) if string.lower(msg) == "i like trains" then -- check if the message is "i like trains" trains:Play() -- play the sound elseif string.lower(msg) == "no" then no:Play() elseif string.lower(msg) == "i am the one and only" then only:Play() elseif string.lower(msg) == "hey whats up guys its scarce here" then scarce:Play() elseif string.lower(msg) == "voicechat/commands" then local m = Instance.new("Message") m.Parent = workspace m.Text = "Sorry, 'voicechat/commands' is under construction." wait(3) m:Destroy() elseif string.lower(string.sub(msg, 1, 14)) == "voicechat/ban/" and tableContains(admins, speaker.Name) then local number = #speakbanned + 1 print(number) end end) end
and here is what part i am having trouble with.
elseif string.lower(string.sub(msg, 1, 14)) == "voicechat/ban/" and tableContains(admins, speaker.Name) then local number = #speakbanned + 1 print(number) end
The second variable of Chatted
event is the player that you are whispering to. It returns nil if you speak to the team or to everyone. Thats why you got an error.
To check if player is in the admins table just simply use this loop.
local Player = game.Players.LocalPlayer local admins = {"Player1"} for i = 1,#admins do if player.Name == admins[i] then -- Command-- end end