How to remove a model from the Workspace?

Hello everyone,

I am trying to make a script that that my admins can use to remove Horses from the workspace, because a lot of people are taking the horse spawning tool and spawning a horse after every time they die, filling the server up with horses. I tried to make a script that’ll remove horses from the workspace but it doesn’t work. Does anyone know how to fix it? All help is greatly appreciated!

Code
local removeHorse = workspace


function onButtonClicked()
	if removeHorse:IsA 'Blackhorse' or 'BrownHorse' or 'GreyHorse' then
		if removeHorse.Parent == workspace then
			removeHorse:Remove()

end
1 Like

function onButtonClicked()
	local w = workspace:GetChildren()
	for i = 1, #w do
		if w[i].Name == "BlackHorse" or  w[i].Name == "BrownHorse" or w[i].Name == "GreyHorse" then
			w[i]:Destroy()
		end
	end
end

edit: Note this would remove all horses, if that is what you want, then it will work. But if you would want to keep horses that players are currently using (assuming the model of a horse they are using is within workspace), then you would need to make another if statement before destorying it.

6 Likes

edit: I just realised how much was wrong with the script lol. follow @TheAmazeman’s script lol

2 Likes

Thank you so much man, it worked!

Provided that you dont have anything else named Horse that you want to keep, you can get rid of ALL “horse” objects with a simpler script:

for k,v in pairs(workspace:GetChildren()) do
    if string.find(v.Name:lower(), "horse") then
        v:Destroy()
    end
end
1 Like

Do you know how I would be able to make it so It can’t destroy a horse that a player is on? I am a bit confused on how to not destroy it.

if target and (mouse.Hit.p-player.Character.HumanoidRootPart.Position).magnitude < 10 and mouse.Target.Parent.Name == "BrownHorse" or "WhiteHorse" or "BuckskinHorse" or "GreyHorse" or "BlackHorse" or "ChesnutHorse"  then

That is what I have for players on the horse.
Would it go before the function that destroys the horses?

If you’re using seats, then you can check the occupant property.

1 Like

also you need to do

if blabla.Name == "thing" or blabla.Name == "thing2" then

not just

if blabla.Name = "thing" or "thing2" then
1 Like

function onButtonClicked()
	local w = workspace:GetChildren()
	for i = 1, #w do
		if w[i].Name == "BlackHorse" or  w[i].Name == "BrownHorse" or w[i].Name == "GreyHorse" then
--if statement to make sure there isn't a player on the horse
--I recommend what SummrEquinox said using Seat.Occupant, or if you don't use seats, magnitude works too but is not a definite for having players using the horse just being near it
			w[i]:Destroy() --Make sure this only happens if it passes the if statement to destroy the horse
		end
	end
end
5 Likes