i have a placement system i made a few hours ago, it works great, but i can place the towers on zombies, i tried using FilterDescendantsInstances to filter the entire enemy folder (the folder where all the alive enemies are in), but i get: Unable to cast value to Object
code:
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local isPlacing = false
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")
local tower = game.ReplicatedStorage.Tower.GroundTower
local CurrentDecoy = nil
print(player:GetDescendants())
function decoy()
local ray = RaycastParams.new()
ray.FilterType = Enum.RaycastFilterType.Blacklist
ray.FilterDescendantsInstances = {player.Character, game.Workspace.Enemies:GetChildren()}
local unitRay = mouse.UnitRay
local rayres = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, ray)
if rayres then
print(rayres.Position)
local Tower = tower:Clone()
Tower.Parent = workspace
Tower.Position = rayres.Position + Vector3.new(0, Tower.Size.Y/2, 0)
Tower.Transparency = .5
Tower.CanCollide = false
return Tower
end
return nil
end
script.Parent.Activated:Connect(function()
isPlacing = true
CurrentDecoy = decoy()
end)
mouse.Move:Connect(function()
if isPlacing == false and CurrentDecoy == nil then return end
local ray = RaycastParams.new()
ray.FilterType = Enum.RaycastFilterType.Blacklist
ray.FilterDescendantsInstances = {player.Character, CurrentDecoy, game.Workspace.Enemies:GetChildren()}
local unitRay = mouse.UnitRay
local rayres = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, ray)
if rayres then
CurrentDecoy.Position = rayres.Position + Vector3.new(0, CurrentDecoy.Size.Y/2, 0)
end
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local ray = RaycastParams.new()
ray.FilterType = Enum.RaycastFilterType.Blacklist
ray.FilterDescendantsInstances = {player.Character, CurrentDecoy, game.Workspace.Enemies:GetChildren()}
local unitRay = mouse.UnitRay
local rayres = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, ray)
if rayres then
pcall(function()
CurrentDecoy.Position = rayres.Position + Vector3.new(0, CurrentDecoy.Size.Y/2, 0)
CurrentDecoy.Transparency = 0
CurrentDecoy.CanCollide = true
CurrentDecoy = nil
end)
end
isPlacing = false
end
end)
end)
if you need more info just comment and i’ll give you some info.