Im trying to make Tower Defense, and im using RaycastFilter, Exclude. But its not working.
The tower is still flying to camera! (I named it as TTs).
I want it stay on ground not fly to the gods!
local uis = game:GetService("UserInputService")
local msPoss
local TTs
local placing = false
local function msraycast()
local msPos = uis:GetMouseLocation()
local cam = workspace.CurrentCamera
local msray = cam:ViewportPointToRay(msPos.X, msPos.Y)
local rParams = RaycastParams.new()
rParams.FilterType = Enum.RaycastFilterType.Exclude
rParams.FilterDescendantsInstances = {TTs}
local raycastR = workspace:Raycast(msray.Origin, msray.Direction * 1000)
return raycastR
end
local function AddPlaceHolderTower(towerName)
local tower = game.ReplicatedStorage.Towers:FindFirstChild(towerName)
if tower then
TTs = tower:Clone()
TTs.Parent = workspace.Towers
end
end
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(tower)
AddPlaceHolderTower(tower)
end)
uis.InputBegan:Connect(function(inp)
if inp.UserInputType == Enum.UserInputType.MouseButton1 then
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if TTs then
local result = msraycast()
if result and result.Instance then
local x = result.Position.X
local y = result.Position.Y + TTs.Humanoid.HipHeight + (TTs.PrimaryPart.Size.Y / 2)
local z = result.Position.Z
local cframe = CFrame.new(x,y,z)
TTs:SetPrimaryPartCFrame(cframe)
end
end
end)
but also if you know how to fix
The TTs (tower to spawn) still half in ground.
local y = result.Position.Y + TTs.Humanoid.HipHeight + (TTs.PrimaryPart.Size.Y / 2)
Here im changing the Y position. But its not working as i expected it to.
can i ask you again, im still working on placement, but its not globalized and creating a total copy of Tower To spawn.
So i need to make Spawn System, This is Local Script, when i click Left Mouse Button, its spawns some kind of Tower, but its spawns A total copy of Tower To Spawn.
LOCAL SCRIPT (“Tower Mananger”)
local uis = game:GetService("UserInputService")
local msPoss
local TTs
local placing = false
local canplace = false
local function msraycast()
local msPos = uis:GetMouseLocation()
local cam = workspace.CurrentCamera
local msray = cam:ViewportPointToRay(msPos.X, msPos.Y)
local rParams = RaycastParams.new()
rParams.FilterType = Enum.RaycastFilterType.Exclude
rParams.FilterDescendantsInstances = {TTs}
local raycastR = workspace:Raycast(msray.Origin, msray.Direction * 1000, rParams)
return raycastR
end
local function AddPlaceHolderTower(towerName)
local tower = game.ReplicatedStorage.Towers:FindFirstChild(towerName)
if tower then
TTs = tower:Clone()
TTs.Parent = workspace.Towers
for i, trash in ipairs(TTs:GetDescendants()) do
if trash:IsA("BasePart") then
trash.Material = Enum.Material.ForceField
trash.CollisionGroup = "Towers"
end
end
end
end
local function ColorPlaceHolderTower(color)
for i, object in ipairs(TTs:GetDescendants()) do
if object:IsA("BasePart") then
object.Color = color
end
end
end
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(tower)
AddPlaceHolderTower(tower)
end)
uis.InputBegan:Connect(function(inp, proc)
if proc then
return
end
if TTs then
if inp.UserInputType == Enum.UserInputType.MouseButton1 and canplace then
game.ReplicatedStorage.SpawnTroop:FireServer(TTs.Name, TTs.PrimaryPart.CFrame)
TTs = nil
end
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if TTs then
local result = msraycast()
if result and result.Instance then
if result.Instance.Parent.Name == "TowerArea" then
ColorPlaceHolderTower(Color3.new(0,1,0))
canplace = true
else
ColorPlaceHolderTower(Color3.new(1,0,0))
canplace = false
end
local x = result.Position.X
local y = result.Position.Y + TTs:GetExtentsSize().Y/2
local z = result.Position.Z
local cframe = CFrame.new(x,y,z)
TTs:SetPrimaryPartCFrame(cframe)
end
end
end)
Script (“Spawner”)
function Spawns(plr, name, cframe)
print(plr, name, cframe)
local tower = game.ReplicatedStorage:FindFirstChild(game.ReplicatedStorage.Towers:FindFirstChild(name))
if tower then
local tts = tower:Clone()
local adjustedCFrame = cframe + Vector3.new(0, 5, 0)
tts:SetPrimaryPartCFrame(adjustedCFrame)
tts.Parent = workspace.Towers
tts.HumanoidRootPart:SetNetworkOwner(nil)
end
end
game.ReplicatedStorage.SpawnTroop.OnServerEvent:Connect(function(plrr, namee, cfr)
Spawns(plrr, namee, cfr)
end)