This is a script that I have been working on that can delete any accessory on a character that is from a group/user on a “Ban List”, a module script that has a table of their respective ID’s. While I am happy that it works, I feel that there are some things that I wish could be better about it.
-
I’ve noticed that sometimes Roblox’s Avatar Service/Editor can go down, and that Player:HasAppearanceLoaded() will not work when something like this occurs. In order to fix this, I’ve wrapped it in a pcall to help combat if something like this occurs. However, I ask: Is this something I should realistically be worried about?
-
The code I have in place to get the names of the accessories on the character itself uses the InsertService to get their names. Is there a better way to get their names that doesn’t involve using InsertService to create a duplicate?
HatBanDemo.rbxl (55.9 KB)
– Main Script
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerScriptService = game:GetService("ServerScriptService")
local InsertService = game:GetService("InsertService")
local BanList = require(ServerScriptService.BanList)
Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
-- // Sometimes (rarely), Avatars can fail to load, or the avatar system can break. To ensure the script does not break when this occurs, wrap :HasAppearanceLoaded in a pcall.
local iteration = 0
local s
while task.wait(0.1) do
s = pcall(function()
return plr:HasAppearanceLoaded()
end)
if s then
break
else if iteration > 60 and s == nil then
-- // If pcall iterates 60 times (approx 6 seconds) with no result, then the avatar service must be broken and the script does not have to be ran.
break
else
-- // Add one to the i variable. If it overflows 61, the loop is broken.
iteration += 1
end
end
end
-- // If the pcall succeeds, this code is ran.
if s then
local desc = char:WaitForChild("Humanoid"):WaitForChild("HumanoidDescription")
local hats = desc:GetAccessories(true)
local hatsToDelete = {}
for i, hat in pairs(hats) do
local assetId = hat.AssetId
local success, result = pcall(function()
return MarketplaceService:GetProductInfo(assetId)
end)
if success then
if result then
-- // Sort if it is made by a Group or User
-- // Users
if result.Creator.CreatorType == "User" then
local UserId = result.Creator.CreatorTargetId
local IsBanned = BanList["BannedCreators"][tonumber(string.sub(tostring(UserId),1,1))][UserId]
if IsBanned then
-- This code demonstrates how there can be a reason demonstrated to the player why their hat is banned, but I'm too lazy rn to make a notification system for this :))))))))
print("Hat " .. result.Name .. " is from a banned user! " .. result.Creator.Name .. " Reason: " .. IsBanned)
local s1, bannedHat = pcall(function()
return InsertService:LoadAsset(assetId)
end)
if s1 then
table.insert(hatsToDelete, bannedHat:FindFirstChildOfClass("Accessory").Name)
bannedHat:Destroy()
end
end
end
-- // Groups
if result.Creator.CreatorType == "Group" then
local GroupId = result.Creator.CreatorTargetId
local IsBanned = BanList["BannedGroups"][tonumber(string.sub(tostring(GroupId),1,1))][GroupId]
if IsBanned then
-- This code demonstrates how there can be a reason demonstrated to the player why their hat is banned, but I'm too lazy rn to make a notification system for this :))))))))
print("Hat " .. result.Name .. " is from a banned group! " .. result.Creator.Name .. " Reason: " .. IsBanned)
local s1, bannedHat = pcall(function()
return InsertService:LoadAsset(assetId)
end)
if s1 then
table.insert(hatsToDelete, bannedHat:FindFirstChildOfClass("Accessory").Name)
bannedHat:Destroy()
end
end
end
end
end
end
-- // The hats that will be removed from the character have been inserted into the hatsToDeleteTable. Now we run through each entry and delete them off the character.
for i, hat in pairs(hatsToDelete) do
local hatOnChar = char:FindFirstChild(hat)
if hatOnChar then
hatOnChar:Destroy()
end
end
end
end)
end)
– Module Script serving as a Ban List
-- local module = {
["BannedCreators"] = {
[1] = {
},
[2] = {
},
[3] = {
},
[4] = {
},
[5] = {
},
[6] = {
},
[7] = {
},
[8] = {
[8115493] = "User creates accessories that cover up the player."
},
[9] = {
}
},
["BannedGroups"] = {
[1] = {
[16142068] = "Group creates accessories that cover up the player.",
[16006645] = "Group creates accessories that cover up the player."
},
[2] = {
},
[3] = {
[32939069] = "Group creates accessories that cover up the player.",
[32836034] = "Group creates accessories that cover up the player."
},
[4] = {
},
[5] = {
},
[6] = {
},
[7] = {
},
[8] = {
},
[9] = {
}
}
}
return module