elseif isn’t recommended to use, in this case. Instead, you should use end just incase the player owns multiple things.
Instead, try this:
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15251997) then
local Tool = script:FindFirstChild('Radio'):Clone()
Tool.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15251965) then
local Tool1 = script:FindFirstChild('BlueGlowstick'):Clone()
Tool1.Parent = Player.Backpack
local Tool2 = script:FindFirstChild('PinkGlowstick'):Clone()
Tool2.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15252032) then
local Tool = script:FindFirstChild('Vibe Gun'):Clone()
Tool.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15252101) then
local Tool = script:FindFirstChild('Selfie Stick'):Clone()
Tool.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 5252157) then
local Tool = script:FindFirstChild('Fidget Spinner'):Clone()
Tool.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15252190) then
local Tool1 = script:FindFirstChild('VibeKatana'):Clone()
Tool1.Parent = Player.Backpack
local Tool2 = script:FindFirstChild('VibeSword'):Clone()
Tool2.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 13354837) then
local Tool = script:FindFirstChild('ChristmasTreeLauncher'):Clone()
Tool.Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15252384) then
script.PoolNoodle:Clone().Parent = Player.Backpack
end
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, 15439621) then
script["Vibe Panda"]:Clone().Parent = Player.Backpack
end
end)
end)
Not completely sure, correct me if my statement is incorrect (pls.):
Wouldn’t the elseif statement skip over the whole if statement once it finds one item a user owns and give them their product? I’ve also heard it also causes more lag to games.
I agree with the top post, using if statements separately within the PlayerAdded event is more efficient.
While the current script version works, it is hard to read and it’s impractical to manage IDs and items when we have a lot of them and want to add even more in the future. Instead, try using this script. All you have to do is change and/or expand the dictionary-type table in the same manner as already present elements are written. I haven’t managed to test this, so you may potentially find an error, but I’m sure you’ll be able to get rid of it quickly.
local Players = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketplaceService")
local GAMEPASS_DATA = {
[15251997] = {"Radio"};
[15251965] = {"BlueGlowstick", "PinkGlowstick"};
[15252032] = {"Vibe Gun"};
[15252101] = {"Selfie Stick"};
[5252157] = {"Fidget Spinner"};
[15252190] = {"VibeKatana", "VibeSword"};
[13354837] = {"ChristmasTreeLauncher"};
[15252384] = {"PoolNoodle"};
[15439621] = {"Vibe Panda"};
}
------------------------------------------------------------------------
local function giveItems(player, character)
local givenItems = {} -- prevents duplication
for id, array in pairs(GAMEPASS_DATA) do
-- Skip if player doesn't own the gamepass.
if (not MarketPlaceService:UserOwnsGamePassAsync(player.UserId, id)) then continue; end
-- Loop through all elements of the array.
for _, v in pairs(array) do
local item = script:FindFirstChild(v)
if (item) then
if (table.find(givenItems, item) == nil) then
item:Clone().Parent = player.Backpack
table.insert(givenItems, item)
end
else
warn("No ".. v .." found")
end
end
end
givenItems = nil -- clean up
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
pcall(giveItems, player, character) -- function + arguments (tuple)
end)
end)