https://streamable.com/m2fsps

local Container = script.Parent.Main:WaitForChild("Container")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote = ReplicatedStorage:WaitForChild("PlaceItem")
local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character or Player.Character:Wait()
local HumanoIdRootPart = Char:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local Items = ReplicatedStorage:WaitForChild("Items")
local RuS = game:GetService("RunService")
local UiS = game:GetService("UserInputService")

local yBuilding = 5
local maxDistance = 50
local IsPlacing = false
local IsKeyPressing = false

for i, item in pairs(Container:GetChildren()) do
	if item:IsA("TextButton") then
		item.MouseButton1Click:Connect(function()
			
			Container.Visible = false
			local readyToPlace = false
			local yorientation = 0 
			local StructurePlaced
			
			if IsPlacing == false then
				IsPlacing = true
				
				local itemToPlace = Items:FindFirstChild(item.Name):Clone()
				itemToPlace.Parent = game.Workspace
				itemToPlace.Material = "Neon"
				itemToPlace.Anchored = true
				itemToPlace.CanCollide = false
				local startingCFrame = CFrame.new(0,0,-15)
				itemToPlace.CFrame = HumanoIdRootPart.CFrame:ToWorldSpace(startingCFrame)
				
				
				local con = RuS.RenderStepped:Connect(function()
					
					local MouseRay = Mouse.UnitRay
					local castRay = Ray.new(MouseRay.Origin, MouseRay.Direction * 1000)
					local list = {itemToPlace, Char}
					local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, list)
					
					if hit and hit:IsA("Part") and (HumanoIdRootPart.Position - itemToPlace.Position).Magnitude < maxDistance then
						readyToPlace = true
						itemToPlace.BrickColor = BrickColor.new("Forest green")
					else
						readyToPlace = false
						itemToPlace.BrickColor = BrickColor.new("Really red")
					end
					
					local newcfangle = CFrame.Angles(0,math.rad(yorientation),0)
					local newcf = CFrame.new(position.X, position.Y + yBuilding, position.Z)
					itemToPlace.CFrame = newcf * newcfangle
				end)
				
				UiS.InputBegan:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						IsKeyPressing = true
						
						local rotation = 5
						while IsKeyPressing and wait() do
							if IsPlacing then
								yorientation = yorientation + rotation
							end
						end
					end
				end)
				UiS.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						IsKeyPressing = false
					end
				end)
				UiS.InputBegan:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						if IsPlacing then
							if readyToPlace then
								
								StructurePlaced = Remote:InvokeServer(itemToPlace.Name, itemToPlace.CFrame)
								
								if StructurePlaced then
									IsPlacing = false
									itemToPlace:Destroy()
									con:Disconnect()
									Container.Visible = true
								end
							end
						end
					end
				end)
			end
		end)
	end
end

2 comments

0

Please add more information to your post about the issue.

0

I provided a video about the issue