Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make this script delete objects with the class name of model but not delete the player model?

Asked by
130363 67
7 years ago

This script is for a clean workspace GUI. Click Here to see the game When you click clean workspace it will delete you(the player) but I want to prevent that. But I don't know how to prevent that. I will deeply appreciate your help.

function onClick()
    local workspaceChildren = game.Workspace:GetChildren()

wait(3)
        for i = 1, #workspaceChildren do
            if workspaceChildren[i].className == "Model" then
                workspaceChildren[i]:Remove()
        end
    end
end

script.Parent.MouseButton1Click:Connect(onClick)

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

You can use GetPlayerFromCharacter to check whether or not the model is associated with a Player object:

local Players = game:GetService('Players');
script.Parent.MouseButton1Click:Connect(function()
    local workspaceChildren = game.Workspace:GetChildren()

    wait(3)
    for i = 1, #workspaceChildren do
        local child = workspaceChildren[i];
        if child.ClassName == "Model" and not Players:GetPlayerFromCharacter(child) then
            child:Destroy()
        end
    end
end)

Hope this helped. Read more about anonymous functions.

PS: className and Remove are deprecated; use ClassName and Destroy instead.

0
I will smash that Accept Answer Button :-) 130363 67 — 7y
Ad

Answer this question