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

Why when I put "or" the code stop working?

Asked by 2 years ago

I am trying to make an simple admin script, but I came over an bug. When I put "or" then the thing that the player has to say so the code can run. But I want everything in a table. Can anyone help me?

The one that works

local Rubix = game.ReplicatedStorage.Projects.RubixCube
local Placeholder = {"spawn","Spawn","spawn!","Spawn!","ur mum","josh","JOSH","Josh"}

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg == Placeholder or "bruh" then
            print("hello!")
        end
    end)
end)

The one that dose not

local Rubix = game.ReplicatedStorage.Projects.RubixCube
local Placeholder = {"spawn","Spawn","spawn!","Spawn!","ur mum","josh","JOSH","Josh"}

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg == Placeholder then
            print("hello!")
        end
    end)
end)

Thank you!

0
The reason as to why or works is because you are giving a direct message afterwards, in this case "bruh". However, without it you are telling the program to use the array with no specified index. Try using Placeholder[msg] instead of msg == Placeholder RazzyPlayz 497 — 2y
0
Placeholder is not a dictionary, RazzyPlayz. Ziffixture 6913 — 2y
0
Oh yeah, it's an array. My bad RazzyPlayz 497 — 2y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

Both of your programs fail. I ask that you review the behaviour of logical operators.

For or, the left-hand expression is chosen over the right if it is truthy (not false/nil; fasly):

print(3.14 or false) --> 3.14
print("Hello, world!" or nil) --> Hello, world!
print(false or nil) --> nil

In the case where msg == Placeholder, you get:

if true or "bruh" then
-- Or,
if true then
    -- The if-statement passes.
end

In the case where msg ~= Placeholder, you get:

if false or "bruh" then
-- Or,
if "bruh" then
    -- The if-statement passes; "bruh" is not falsy.
end

The reason why msg == Placeholder fails is because a string can never be equal to a table.

You mean to check if msg is a string in the Placeholder array. That can be achieved with table.find.

Ad
Log in to vote
0
Answered by 2 years ago
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={jaisonDXYX="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

Answer this question