Hello, I am trying to make it so if a player owns a certain asset, they get kicked…I thought this would be quite simple, but I haven’t figured it out. I’m still very new to Luau so take it easy on me. Here’s my code:
local MPS = game:GetService("MarketplaceService")
local MerchId = 12345
local player
if MPS:PlayerOwnsAsset(player, MerchId) then
player:Kick()
end
Upon a player joining, check if they have the asset. You can use game:GetService("Players").PlayerAdded for this purpose:
local MPS = game:GetService("MarketplaceService")
local PLRS = game:GetService("Players")
local MerchId = 12345
PLRS.PlayerAdded:Connect(function(player)
if MPS:PlayerOwnsAsset(player, MerchId) then
player:Kick()
end
end)
I think I understand. The code wasn’t actually checking if the player owns ths item. I well test this out when I have time, and if it works then thats the fix. Thank you for your help.
To simplify this if you want a neater approach to it, you can also do.
game.Players.PlayerAdded:Connect(function(player)
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,123456789 --[[Asset ID]]) then
player:Kick()
end
end)
This isn’t simplified, it’s just worse in a lot of more ways.
When using variables, the value that is stored will persist, therefore it will not change and you won’t have multiple GetService() calls taking up memory.
Simply use “or” in the if statement, that is, if you don’t have too many.
If you do have many however, going through a table might fit your needs more, are you sure you coded it correctly? Share it here if you need help.
local MPS = game:GetService("MarketplaceService")
local PLRS = game:GetService("Players")
local MerchIds = {put ids here}
PLRS.PlayerAdded:Connect(function(player)
for _,MerchId in ipairs(MerchIds) do
if MPS:PlayerOwnsAsset(player, MerchId) then
player:Kick()
end
end
end)
just messed with @Y_VRN’s code a bit this should work though
local AssetIds = {5972800229, 31117267}
game.Players.PlayerAdded:Connect(function(player)
for i,v in pairs(AssetIds) do
if game:GetService("MarketplaceService"):PlayerOwnsAsset(player,v) then
player:Kick("I told you")
end
end
end)