Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

1 comment

0

I will smash that Accept Answer Button :-)

This discussion is preserved as it was. New answers, comments, and votes are closed.