Placement only works once

  1. What do you want to achieve? Keep it simple and clear!
    For the placement to work continuously

  2. What is the issue? Include screenshots / videos if possible!
    The placement only works once. It used to work before, but now It just doesn’t:
    robloxapp-20220217-1944010.wmv (4.4 MB)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    The issue can’t be found. I tried printing everything and I’m pretty sure it isn’t a conditional issue.

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")

local getMap = ReplicatedStorage.SelectedMap
if not getMap.Value then
	getMap:GetPropertyChangedSignal("Value"):Wait()
end

local getReplicatedMap = ReplicatedStorage.Maps:FindFirstChild(tostring(getMap.Value))
if not getReplicatedMap then
	error("REPLICATED MAP: "..getMap.Name.." DOES NOT EXIST")
end

local PlacementEvents = ReplicatedStorage:WaitForChild("PlacementEvents")
local PlacementEventsMain = PlacementEvents:WaitForChild("Main")
local spawnTowerEvent = PlacementEventsMain:WaitForChild("SpawnTower")


local Camera = workspace.Camera
local gui = script.Parent
local TowerToSpawn = nil
local CanPlace = false
local Rotation = 0

local function MouseRaycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	
	local mouseRay = Camera:ViewportPointToRay(mousePosition.X, mousePosition.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

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return --gameProcessed means inputed in different object e.g chat
	end
	if TowerToSpawn then 
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if CanPlace then
				spawnTowerEvent:FireServer(TowerToSpawn.Name, TowerToSpawn.PrimaryPart.CFrame)
				RemovePlaceholderTower()
			end
		elseif input.KeyCode == Enum.KeyCode.R then
			Rotation += 90
		end
	end
end)

function RemovePlaceholderTower()
	if TowerToSpawn then
		TowerToSpawn:Destroy()
		TowerToSpawn = nil
		Rotation = 0
	end
end


local function AddPlaceHolderTower(name : string)
	local towerExists = getReplicatedMap.Towers:FindFirstChild(tostring(name))
	if towerExists then
		TowerToSpawn = towerExists:Clone()
		TowerToSpawn.Parent = workspace.Towers.PlacingTowers
		
		for i, object in ipairs(TowerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
			end
		end
	end
end

local function ColorPlaceholderTower(color)
	for i, object in ipairs(TowerToSpawn:GetDescendants()) do
		if object:IsA("BasePart") then
			object.Color = color
		end
	end
	if not CanPlace then
		TowerToSpawn.Range.Transparency = 1
	else
		TowerToSpawn.Range.Transparency = 0
	end
end

gui["1"].MainButton.Activated:Connect(function()
	AddPlaceHolderTower("Tower")
end)


RunService.RenderStepped:Connect(function()
	if TowerToSpawn then
		local result = MouseRaycast({TowerToSpawn})
		if result and result.Instance then
			if result.Instance.Parent.Name == "TowerArea" then
				CanPlace = true
				ColorPlaceholderTower(Color3.new(0,1,0))
			else
				CanPlace = false
				ColorPlaceholderTower(Color3.new(1,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)
			TowerToSpawn.Range.Size = Vector3.new(0.1, TowerToSpawn.Levels[TowerToSpawn.CurrentLevel.Value].Range.Value * 2, TowerToSpawn.Levels[TowerToSpawn.CurrentLevel.Value].Range.Value * 2)
		end
	end
end)

Range BasePart was not apart of the “Tower” physics service. This now works!

1 Like