Kicking if a player owns a certain asset

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

Set the ID to the specific asset you want to block players with. And an end to your if code.

if MPS:PlayerOwnsAsset(player, MerchId) then
player:Kick()
end -- Here.

I understand that, I do have the correct ID. This was just an example. And the end, I forgot to add to the post…Sorry to waste your time

1 Like

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)
1 Like

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.

1 Like

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)
2 Likes

Use the player added event on the server.

This seems to work, the script that @SuguruDev suggested is much more simple, so I went for that. Thank you for the reply though

put all the asset ids into a table then loop through them and check if the player owns any

Thats exactly what I did. Thanks for the reply though

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

2 Likes

I already figured this out. I am gonna be using tables. Thanks for the reply though!

I used a VERY similar script

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)

So should I add a variable for MarketplaceService?

Since you’re calling PlayerOwnsAsset multiple times, then yes you should, as not only you’re calling that function, but also GetService.

Thank you so much for all the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.