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

It says it's a nil value but it's not? [SOLVED]

Asked by
ARTEK22 135
8 years ago
Edited 8 years ago

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

1 answer

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

The second variable of Chattedevent 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
0
I want it to have admin commands and nonadmin commands. As an admin i won't be able to use nonadmin commands. ARTEK22 135 — 8y
0
But you kinda gave me an idea. Instead of getting the player from the chatted event, im going to get it from the GetChildren one! ARTEK22 135 — 8y
0
Make sure to accept my answer. personal_ly 151 — 8y
Ad

Answer this question