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

ServerScriptService.Script:9: attempt to index nil with 'Chatted'?

Asked by 1 year ago
Edited 1 year ago
function OnChatted(msg, speaker)
    local source = string.lower(speaker.Name)
    msg = string.lower(msg)
    if msg == ":sit" then
        speaker.Character.Humanoid.Sit = true
    end
end

game.Players.LocalPlayer.Chatted:Connect(OnChatted)

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg) OnChatted(msg, player)
    end)
end)

Why it says like that?

1 answer

Log in to vote
0
Answered by 1 year ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
local Players=game:GetService"Players"
local prefix,cmds,findtarget,debounce,getmsg=":",nil,function(player,name)
    name=name or "me"
    local target;
    if name=="me"then
        target=player
    else
        for _,v in ipairs(Players:GetPlayers())do
            if v.Name:lower():sub(1,name:len())==name then
                target=v;break;
            end
        end
    end
    return target
end,{},function(tab)
    local str=""
    for i,v in ipairs(tab)do
    str=str.." "..v
    end
    return str
end
local admins,onChatted={RektwayYTB="Owner",Builderman="Admin",Noob="TempAdmin"},function(player,msg)
    if msg:sub(1,1)==prefix and msg:len()>1 then
        msg=msg:lower():sub(2,msg:len())
        local tab=string.split(msg," ")
        local cmd,arg1=tab[1],tab[2]
        table.remove(tab,1)table.remove(tab,1)
        cmd=cmds[cmd]
        if not cmd then return end
        local arg1s=arg1
        arg1=findtarget(player,arg1)or arg1
        cmd(player,type(arg1)~="string"and arg1 or nil,(arg1s or "")..getmsg(tab))
    end
end

cmds={
    ff=function(player,target)if not target then return end;Instance.new("ForceField",target.Character)end,
    unff=function(player,target)
        if not target then return end
        local char=target.Character
        local ff=char and char:FindFirstChildOfClass"ForceField"
        if ff then ff:Destroy()end
    end,
    respawn=function(player,target)if not target then return end;target:LoadCharacter()end,
    kick=function(player,target,msg)if target and target~=player then target:Kick(msg or "")end;end,
    kill=function(player,target)
        if not target then return end
        local char=player.Character
        local hum=char and char:FindFirstChildOfClass"Humanoid"
        if hum and hum.Health>0 then hum.Health=0 end
    end,
    punish=function(player,target)
        if not target then return end
        local char=target.Character
        if char then char.Parent=nil end;
    end,
    h=function(player,target,msg)
        local m=Instance.new("Hint",workspace)
        m.Text=msg
        wait(2)
        m:Destroy()
    end,
    m=function(player,target,msg)
        local m=Instance.new("Message",workspace)
        m.Text=msg
        wait(2)
        m:Destroy()
    end,
    admin=function(player,target,msg) 
        if not target or target==player then return end
        admins[target.Name]="TempAdmin"
    end,
    pa=function(player,target,msg) 
        if admins[player.Name]=="Owner"and target and target~=player then
        admins[target.Name]="Admin"end
    end,
    adminlist=function()for i,v in pairs(admins)do print(i,v)end;end; 

}
local function onPlayerAdded(player)
    if admins[player.Name]then
        player.Chatted:Connect(function(msg)
            if debounce[player.Name]then return end
            debounce[player.Name]=true
            pcall(onChatted,player,msg)
            wait(1)
            debounce[player.Name]=false
        end)
    end
end;Players.PlayerAdded:Connect(onPlayerAdded)
for _,v in ipairs(Players:GetPlayers())do
    onPlayerAdded(v)
end
0
This code is disgusting. Ziffixture 6913 — 1y
Ad

Answer this question