I’ve been working on Bloxy Hero Defense (My current project) for a while, and (as the title states) my placement system is breaking.
I’m a bit new to raycasting, so please notify me in the replies if you see anything wrong with it.
Placement system (Server):
remotes.isPlacingEvent.OnServerEvent:Connect(function(plr, x, z, y, mouseOrigin)
local blackList = {}
local tower = toPlace:GetChildren()
local primaryPart
for i, part in ipairs(tower) do
if part.Name == "Tower" or part.Parent.PrimaryPart == part then primaryPart = part end
table.insert(blackList, part)
end
for i, v in ipairs(game.Players:GetPlayers()) do
local char = v.Character or v.CharacterAdded:Wait()
for j, part in ipairs(char:GetChildren()) do
if part:IsA("BasePart") then
table.insert(blackList, part)
end
end
end
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = blackList
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local rayCast2 = game.Workspace:Raycast(mouseOrigin, Vector3.new(0, -10, 0), raycastParams)
local raycastResult2
if rayCast2 ~= nil then
print(rayCast2)
raycastResult2 = rayCast2.Instance
elseif rayCast2 == nil then
raycastResult2 = game.ServerStorage.BaseplateCopy
end
if raycastResult2.Parent == game.Workspace.AllowedToPlaceOn then
for i, part in ipairs(toPlace:GetChildren()) do
if hasProperty(part, "UsePartColor") then
part.UsePartColor = false
elseif hasProperty(part, "BrickColor") then
part.BrickColor = rangeColor
else
continue
end
end
ableToPlace = true
remotes.SendPlacementStatus:FireClient(plr, ableToPlace)
else
print(raycastResult2)
for i, part in ipairs(toPlace:GetChildren()) do
if hasProperty(part, "UsePartColor") then
part.UsePartColor = true
elseif hasProperty(part, "BrickColor") then
part.BrickColor = BrickColor.new("Really red")
else
continue
end
end
print("You are not allowed to place here")
ableToPlace = false
remotes.SendPlacementStatus:FireClient(plr, ableToPlace)
end
placeFunction(toPlace, x, z, raycastResult2, y)
end)
Local script:
local repStorage = game:GetService("ReplicatedStorage")
local remotes = require(repStorage:WaitForChild("RemoteModule"))
local runService = game:GetService("RunService")
local isPlacing = false
local UserInputService = game:GetService("UserInputService")
local plrService = game:GetService("Players")
local PlaceConnection
local RotateConnection
local currentTowerBeingPlaced = nil
local plr = plrService.LocalPlayer
local rotateDebounce = false
local tweenService = game:GetService("TweenService")
local ableToPlace
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false)
script.Parent.MouseButton1Up:Connect(function()
remotes.SendPlacementStatus.OnClientEvent:Connect(function(status)
ableToPlace = status
end)
print("Clicked!")
print(isPlacing)
if isPlacing == false then
isPlacing = true
local mouse = plr:GetMouse()
local tower = repStorage:WaitForChild(script.Parent.Name)
remotes.startPlacing:FireServer(tower)
remotes.SendTower.OnClientEvent:Connect(function(towerE)
mouse.TargetFilter = towerE
currentTowerBeingPlaced = towerE
print(currentTowerBeingPlaced)
end)
print("Fired from client!")
PlaceConnection = runService.RenderStepped:Connect(function()
remotes.isPlacingEvent:FireServer(mouse.Hit.Position.X, mouse.Hit.Position.Z, mouse.Hit.Position.Y, mouse.UnitRay.Origin)
end)
remotes.SetTransparencyToOtherClients.OnClientEvent:Connect(function(transparency, towerE)
for i, part in ipairs(towerE:GetChildren()) do
part.Transparency = transparency
end
end)
RotateConnection = UserInputService.InputBegan:Connect(function(input, gpu)
if gpu then return end
if input.KeyCode == Enum.KeyCode.R then
if not rotateDebounce then
rotateDebounce = true
print(currentTowerBeingPlaced)
remotes.RotateOnServer:FireServer()
local tween = tweenService:Create(currentTowerBeingPlaced.PrimaryPart, tweenInfo, {Orientation = currentTowerBeingPlaced.PrimaryPart.Orientation + Vector3.new(0, 90, 0)})
tween:Play()
tween.Completed:Wait()
rotateDebounce = false
end
end
end)
local downConnection = mouse.Button1Down:Connect(function()
if ableToPlace then
remotes.place:FireServer()
RotateConnection:Disconnect()
PlaceConnection:Disconnect()
isPlacing = false
else
print("Cannot place tower")
end
end)
end
end)