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

I have a script that kicks people out of the game that don't have the shirt but it's not working?

Asked by 6 years ago

My kick script checks if the player has the shirt or not, and if they don't they get kicked, if they do they stay. But the problem is that it only checks the first shirt. It doesn't check for the other shirts.

local ShirtId1 = 2620211336 
local ShirtId2 = 2620213086 
local ShirtId3 = 2620212616 

local Shirt_Name = "Group shirt"

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsShirt = MarketplaceService.PlayerOwnsAsset


Players.PlayerAdded:Connect(function (player)
    local Success, doesPlayerOwnShirt = pcall(PlayerOwnsShirt, MarketplaceService, player, ShirtId1 or ShirtId2 or ShirtId3)
    if doesPlayerOwnShirt then
        print(player.Name.. " owns " .. Shirt_Name)
    else
        player:Kick("Sorry") 
        print(player.Name .. " doesn't own " .. Shirt_Name)
    end
end)

1 answer

Log in to vote
0
Answered by
clc02 553 Moderation Voter
6 years ago

"or" has a few uses outside of conditionals. If you have a function that takes an argument, but it can also be sent without, you can set a default value with or like:

function foo(var)
  var = var or 10
end

Var will stay var, unless there is no var, in which case it will be 10.

This is what's happening in:

pcall(PlayerOwnsShirt, MarketplaceService, player, ShirtId1 or ShirtId2 or ShirtId3)

ShirtId1 exists, so it skips over the or and stays itself. You'll have to iterate through checks with each shirt on a separate check.

Here's how I'd do it

local shirtIds = { 2620211336, 2620213086, 2620212616 }
local Shirt_Name = "Group shirt"

local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsShirt = MarketplaceService.PlayerOwnsAsset


Players.PlayerAdded:Connect(function (player)
    local doesPlayerOwnShirt = false
    for i, v in pairs(shirtIds) do
      local success = false
      repeat
        success, check = pcall(PlayerOwnsShirt, MarketplaceService, player, v)
        if not success then wait(1) end
      until (success == true)
      if check then doesPlayerOwnShirt = true break end
    end
    if doesPlayerOwnShirt then
        print(player.Name.. " owns " .. Shirt_Name)
    else
        player:Kick("Sorry") 
        print(player.Name .. " doesn't own " .. Shirt_Name)
    end
end)
0
line 14, check is underlined in blue. It's not defined. Where should I define it? Timbawolf1 6 — 6y
0
My bad, throw it after line 12, local check = nil or just local check clc02 553 — 6y
Ad

Answer this question