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

Issue with loop and matching strings-overall I think I'm doing this wrong?

Asked by
unmiss 337 Moderation Voter
9 years ago

This is supposed to check Trello if your name exists on the weapon ban list, and if it does, do nothing. If it doesn't, you're supposed to be given the weapon. I really don't understand why I can't figure this out but I'm getting quite confused-three cards, two with playername:playerid. Help? I'm braindead:

local wep = game.ServerStorage.Revolver
local mps = game:GetService("MarketplaceService")
local api = require(game.ServerScriptService.TrelloAPI)

local notallowed
local n
local i

local pass1 = 225096688 
local pass2 = 245915579 
--local allowed = {
--  [BrickColor.new("White")] = true;
--  [BrickColor.new("Medium stone grey")] = true;
--  [BrickColor.new("Cyan")] = true
--}
-- tried to do this but it wasnt working. sorry bout that




game.Players.PlayerAdded:connect(function(plr)
    print("player is added gg")
    plr.CharacterAdded:connect(function(char)

        local board = api:GetBoardID("Broadview Research and Development")
        local listid = api:GetListID("Testing",board)
        local list = api:GetCardsInList(listid,board)

        print("added yo!!")
        if plr.TeamColor == BrickColor.new("White") or plr.TeamColor == BrickColor.new("Medium stone grey") or plr.TeamColor == BrickColor.new("Cyan") and mps:PlayerOwnsAsset(plr,pass1) or mps:PlayerOwnsAsset(plr,pass2) then print("yay ok")
            for _,c in pairs(list) do
                print("ok then")
                if c.name:match('(.*):(.*)') then
                    n,i = c.name:match('(.*):(.*)')
                    print(n)
                    print(i)
                    if n == plr.Name or i == plr.userId then
                        print(plr.Name .. " is banned from revolver use")
                        break
                    else

                        wep:Clone().Parent = plr.Backpack
                    end
                end
            end
        end
    end)
end)

Lots of inefficient things that I know how to do but simply wouldn't work. This might need someone experienced to answer. Essentially, the list is to be used as shown here I usually get multiple revolvers even though my name is there. I know I'm doing this completely incorrect but honestly don't know what to do. API is by nstrike159.

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The problem is that you taking a definitive action for each thing in list: you have an else on line 40.

In reality, the else block should only happen if none of the if blocks match -- not on each.

Record whether or not they're a bad user, and act after the loop instead of during:

local getsWeapon = true -- innocent until proven guilty
for _, bannedLine in pairs(list) do
    local name, id = bannedLine:match("(.*):(.*)")
    if name then
        if name:lower() == plr.Name:lower() or id == tostring(plr.userId) then
            getsWeapon = false -- They're on the bad list
        end
    end
end

if getsWeapon then
    wep:Clone().Parent = plr.Backpack
else
    print(plr.Name .. " is banned from revolver use")
end

There were two minor tweaks I made:

  • I made the name check case-insensitive by :lower() on both
  • I used tostring on userId. This one is necessary -- 5 ~= "5", but userId is a number while id will be a string (since it's a result of string.match)
Ad

Answer this question