I've made a grid placing system and I have tried to make it so I click a button and while the amount of blocks is >= 1 I can place a block, the issue is after I place 4 - 5 blocks the placing system which is the green structure goes crazy and kinda spazzes out. In writing this I am also aware that the script below (local script) does not change the value of the amount but that isn't my issue right now. Thanks!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local mouse = player:GetMouse()

local isClicked = false
local isEnabled = false

local gridSize = 2
local yBuildingOffset = 2
local maxPlacingDistance = 50
local rKeyIsPressed = false
local placingStructure = false


for _, StructureButton in pairs(StructureFrame:GetChildren()) do
	if StructureButton:IsA("TextButton") then
		StructureButton.MouseButton1Up:Connect(function()
			local Amount = Structures:FindFirstChild(StructureButton.Name):FindFirstChild("Amount").Value
			
			if isEnabled == false and Amount >= 1 then
				isEnabled = true 
				
				while isEnabled == true do
					wait(1)
			StructureFrame.Visible = true
			
			local yOrientation = 5
			local goodToPlace = false
			local placedStructure 
			
			if placingStructure == false then
				placingStructure = true
				
				local clientStructure = Structures:FindFirstChild(StructureButton.Name):Clone()
				clientStructure.BrickColor = BrickColor.new("Forest green")
				clientStructure.Material = "Neon"
				clientStructure.CanCollide = false
				clientStructure.Parent = game.Workspace
				
				local startingCFrame = CFrame.new(0, -2, -15)
				clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
				
				RunService.RenderStepped:Connect(function()
					local mouseRay = mouse.UnitRay
					local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
					local ignoreList = {clientStructure, char}
					local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
					
					if hit and (hit:IsA("Part")) and (HumanoidRootPart.Position - clientStructure.Position).magnitude < maxPlacingDistance then
						goodToPlace = true
						clientStructure.BrickColor = BrickColor.new("Forest green")
					else
						goodToPlace = false
						clientStructure.BrickColor = BrickColor.new("Crimson")
					end
					local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
					local newCFrame = CFrame.new(math.floor(position.X / gridSize) * gridSize, math.floor(position.Y / gridSize) * gridSize + yBuildingOffset, math.floor(position.Z / gridSize) * gridSize)
					clientStructure.CFrame = newCFrame
				end)
				
				UIS.InputBegan:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						rKeyIsPressed = true
						
						local rotationSpeed = 0
						while rKeyIsPressed do
							wait()
							if placingStructure == true then
								yOrientation = yOrientation + rotationSpeed
							end
						end
					end
				end)
				UIS.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.R then
						rKeyIsPressed = false
					end
				end)
				
				UIS.InputBegan:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						if isClicked == false then
						isClicked = true
						if placingStructure == true then
							if goodToPlace == true then
								local StructureCFrame = clientStructure.CFrame
								placedStructure = PlaceStructure:InvokeServer(clientStructure.Name, StructureCFrame)
								Amount -= 1
								if placedStructure == true then
									placingStructure = false
									clientStructure:Destroy()
											StructureFrame.Visible = true
										end
								end
							end
						end
					end
				end)
				UIS.InputEnded:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton1 then
						isClicked = false
					end
					
				end)
				end
				end
			else
				isEnabled = false
			end
		end)
	end
	end

2 comments

0

can you post an image or a video in a website and send it here?

0

https://streamable.com/cnsesp here is a video, look at the end