Hello. I wanted to make a clean debris pad queue, which if 80% of the players step on it, the debris/unanchored parts would be cleaned, but it would not work.
Script:
local playersSteppingOnBluePad = 0
if playersSteppingOnBluePad >= math.floor(#game.Players:GetPlayers()/0.8) then
function Clean(Brick)
if Brick.className == "Part" then
wait()
if Brick.Anchored or Brick.Locked then
return
end
Brick.Parent = Instance.new("Part"):FindFirstChild("Nothing") -- Returns nil, right?
end
end
local Bricks = game.Workspace:GetChildren()
for X = 1, # Bricks do
Clean(Bricks[X])
end
game.Workspace.ChildAdded:connect(Clean)
end
How are you updating the value of playersSteppingOnBluePad?
Do all the players have to be simultaneously stepping on the pad, or does it save that they stepped on it until the threshold is reached?
Why do you need to have this feature in the first place? Wouldn’t it be better to clear debris on an interval?
What’s the purpose of clearing all parts when the script starts, and clearing the part when it’s added to workspace? Do you never want these parts to exist? If so, why have this voting system?
This may be a little verbose, but I wrote it to be clearly understandable. See if you can learn what you need from this complete example:
--!strict
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local votingPad = Workspace.BluePad
local votingPlayers: { [Player]: boolean } = {}
-- Proportion of players who need to have touched the voting pad since debris was last cleared
-- to clear debris again
local THRESHOLD_PROPORTION = 0.80
-- Returns the number of key/value pairs in a dictionary
local function getCount(dictionary: { [any]: any })
local count = 0
for _, __ in pairs(dictionary) do
count += 1
end
return count
end
-- Returns the player with a character that is an ancestor of the given instance,
-- or nil if no player is found
local function getPlayerFromInstance(instance: Instance)
local player: Player?
while instance.Parent and not player do
player = Players:GetPlayerFromCharacter(instance.Parent)
instance = instance.Parent
end
return player
end
-- Returns true if the given instance is to be considered Debris to be cleared
local function isDebris(instance: Instance)
if not instance:IsA("BasePart") then
return false
end
if instance.Anchored then
return false
end
if instance.Locked then
return false
end
return true
end
-- Removes all debris that is a direct child of Workspace and clears the
local function clearDebris()
for _, loosePart in ipairs(Workspace:GetChildren()) do
if not isDebris(loosePart) then
continue
end
loosePart:Destroy()
end
-- Reset the voting players table after clearing debris, so players need to vote again
votingPlayers = {}
end
local function checkVotingThreshold()
local minPlayerVotesRequired = math.floor(#Players:GetPlayers() * THRESHOLD_PROPORTION)
if getCount(votingPlayers) >= minPlayerVotesRequired then
clearDebris()
end
end
local function addPlayerVote(player: Player)
if votingPlayers[player] then
-- Player already voted, no need to add their vote again
return
end
votingPlayers[player] = true
checkVotingThreshold()
end
local function removePlayerVote(player: Player)
if not votingPlayers[player] then
-- Player didn't vote, no need to remove their vote
return
end
votingPlayers[player] = nil
-- The proportion of voting players has now changed, re-check if the threshold is met
checkVotingThreshold()
end
local function onPadTouched(hitPart: BasePart)
local player = getPlayerFromInstance(hitPart)
if not player then
return
end
addPlayerVote(player)
end
local function onChildAdded(instance: Instance)
-- Deferred to avoid changing the parent immediately when it's reparented
-- (destroying on the same cycle it's reparented on throws a warning and skips destroying,
-- using defer calls the function on the next cycle to avoid this)
task.defer(clearDebris)
end
Players.PlayerRemoving:Connect(removePlayerVote)
votingPad.Touched:Connect(onPadTouched)
-- With the addition of these two lines, you should be able to get rid of the entire voting system,
-- because debris will be instantly destroyed, but I'm including the code for the whole voting system
-- for learning purposes anyway
Workspace.ChildAdded:Connect(onChildAdded)
clearDebris()
Note the last couple lines and the associated comment: The whole voting system shouldn’t be necessary if you’re clearing debris as soon as it’s added. If you want to keep the voting system to allow debris to accumulate until a vote passes, then comment out or remove the last two lines.
Also note that this only clears debris if it’s a direct child of Workspace, i.e. if it’s inside a folder or a model or something, this won’t destroy it.