Alright, thanks both of you but my placement system still doesnât work, I need your help
Since I donât know how to make a Placement system I watched GnomeCodeâs tutorial I customized the system a little bit
Also, I got a new error after I used Primitivedâs code
(Iâm so sorry if I wasted everybodyâs time)
System (Main Script) (Local Script)
local PhysicsEngine = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Remotes = ReplicatedStorage:WaitForChild("Remote")
local PlaceTowerEvent = Remotes:WaitForChild("PlaceTower")
local TowerInfoEvent = Remotes:WaitForChild("TowerInfo")
local TowersFolder = ReplicatedStorage:WaitForChild("Towers")
local Camera = workspace.CurrentCamera
local Gui = script.Parent
local TowerToSpawn = nil
local CanPlace = false
local Rotation = 0
local function MouseRaycast(Blacklist)
local MousePos = UserInputService:GetMouseLocation()
local Mouseray = Camera:ViewportPointToRay(MousePos.X, MousePos.Y)
local Raycastparams = RaycastParams.new()
Raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
Raycastparams.FilterDescendantsInstances = Blacklist
local RaycastResult = workspace:Raycast(Mouseray.Origin, Mouseray.Direction * 1000, Raycastparams)
return RaycastResult
end
local function RemovePlaceholder()
if TowerToSpawn then
TowerToSpawn:Destroy()
TowerToSpawn = nil
Rotation = 0
end
end
local function AddPlaceholderTower(Name)
TowerInfoEvent.OnClientEvent:Connect(function(TowerName)
local ExistingTowers = TowersFolder:FindFirstChild(TowerName)
if ExistingTowers then
RemovePlaceholder()
TowerToSpawn = ExistingTowers:Clone()
TowerToSpawn.Parent = workspace.Towers
TowerToSpawn.Outline.OutlineColor = TowerName
end
end)
end
local function ColorPlaceholder(color)
for i, Object in ipairs(TowerToSpawn:GetDescendants()) do
if Object:IsA("BasePart") then
PhysicsEngine:SetPartCollisionGroup(Object, "Tower")
Object.Color = color
end
end
end
TowerInfoEvent.OnClientEvent:Connect(function(Player, TowerName)
Gui.TextButton.Activated:Connect(function(P)
AddPlaceholderTower(TowerName)
end)
end)
UserInputService.InputBegan:Connect(function(Input, Processed)
if Processed then
return
end
if TowerToSpawn then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if CanPlace then
PlaceTowerEvent:FireServer(TowerToSpawn.Name, TowerToSpawn.PrimaryPart.CFrame)
RemovePlaceholder()
end
elseif Input.KeyCode == Enum.KeyCode.R then
Rotation += 90
elseif Input.KeyCode == Enum.KeyCode.Q then
Rotation -= 90
end
end
end)
RunService.RenderStepped:Connect(function()
if TowerToSpawn then
local Result = MouseRaycast({TowerToSpawn})
if Result and Result.Instance then
if Result.Instance.Parent.Name == "TowersArea" then
CanPlace = true
ColorPlaceholder(Color3.fromRGB(255, 255, 255))
else
CanPlace = false
ColorPlaceholder(Color3.fromRGB(255, 0, 0))
end
local X = Result.Position.X
local Y = Result.Position.Y + TowerToSpawn.Humanoid.HipHeight + (TowerToSpawn.PrimaryPart.Size.Y /2)
local Z = Result.Position.Z
local Cframe = CFrame.new(X, Y, Z) * CFrame.Angles(0, math.rad(Rotation), 0)
TowerToSpawn:SetPrimaryPartCFrame(Cframe)
end
end
end)
Signal Sender (Inside of PlaceButton GUI) (Server Script)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remote")
local PlaceTowerEvent = Remotes:WaitForChild("PlaceTower")
local TowerInfoEvent = Remotes:WaitForChild("TowerInfo")
local TowersFolder = ReplicatedStorage:WaitForChild("Towers")
local SignalEvent = script.Parent.Signal
SignalEvent.OnServerEvent:Connect(function(TowerName)
local ConfirmedTowers = TowersFolder:FindFirstChild(TowerName)
local ConfirmedTowersName = ConfirmedTowers.Name -- attempt to index nil with 'Name'
TowerInfoEvent:FireClient(ConfirmedTowersName)
end)
Signal Retriever (Client) (Also inside of PlaceButton)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remote")
local PlaceTowerEvent = Remotes:WaitForChild("PlaceTower")
local TowerInfoEvent = Remotes:WaitForChild("TowerInfo")
local TowersFolder = ReplicatedStorage:WaitForChild("Towers")
local SignalEvent = script.Parent.Signal
script.Parent.Activated:Connect(function()
SignalEvent:FireServer("Outrider")
end)
Tower Module (Inside of serverscriptservice)
local Towers = {}
local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TowersFolder = ReplicatedStorage.Towers
local Remotes = ReplicatedStorage:WaitForChild("Remote")
local PlaceTowerEvent = Remotes:WaitForChild("PlaceTower")
function Towers.PlaceDown(Player, TowerName, Cframe)
local WantedTower = Towers:FindFirstChild(TowerName)
if WantedTower then
local ClonedTower = WantedTower:Clone()
ClonedTower.HumanoidRootPart.CFrame = Cframe
ClonedTower.Parent = workspace.Towers
ClonedTower.HumanoidRootPart:SetNetworkOwner(nil)
for i, EveryParts in ipairs(ClonedTower:GetDescendants()) do
if EveryParts:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(EveryParts, "Towers")
end
end
end
end
PlaceTowerEvent.OnServerEvent:Connect(Towers.PlaceDown)
return Towers