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
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.
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?
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