Is there something wrong with my Code?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

    I want to figure out what is the problem is with my code and why it is not working

  2. What is the issue?

    I don’t know the problem with my code

  3. What solutions have you tried so far?
    Proofreading my code if there is anything causing the issue


I am trying to make a placement system for my Roblox game. It is expected to show a client-sided preview of the model that it is supposed to place, then it will place it.
The problem is that the model doesn’t clone at all for some reason even with commands that will instruct it to clone it.

Here is the script, client, and server-sided

Client:

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

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

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

local yBuildingOffset = 1
local MaxBuildingdistance = math.huge
local zKeyIsPressed = false
local PlacingStructure = false
local yOrientation = 0
local rKeyIsPressed = false

for _, structurebutton in pairs(StructureFrame:GetChildren()) do
	if structurebutton:IsA("TextButton") then
		structurebutton.MouseButton1Up:Connect(function()
			StructureFrame.Visible = false
			
			local yOrientation = 0
			local goodToPlace = false
			local placedStructure = false
			
			if PlaceStructure ==  false then
				PlacingStructure = true
				
				local cilentstructure = Structures:FindFirstChild(structurebutton.Name):Clone()
				cilentstructure.Brickcolor = BrickColor.new("Forest green")
				cilentstructure.Material = "Neon"
				cilentstructure.CanCollide = false
				cilentstructure.Parent = game.Workspace
				
				local StartingCFrame = CFrame.new(0, -2, - 15)
				cilentstructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingCFrame)
				
				RunService.RenderStepped:Connect(function()
					local mouseRay = Mouse.UnitRay
					local castray = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
					local ignorelist = {cilentstructure,char}
					local hit, position = workspace:FindPartsOnRayWithIgnoreList(castray, ignorelist)
					
					if hit then
						goodToPlace = true
						cilentstructure.BrickColor = BrickColor.new("Forest green")
					else
						goodToPlace = false
						cilentstructure.BrickColor = BrickColor.new("Crimson")
					end
					
					local newanglesCFrame = CFrame.new(0, math.rad(yOrientation), 0)
					local newCFrame = CFrame.new(position.X,position.Y + yBuildingOffset, position.Z)
					cilentstructure.CFrame = newCFrame * newanglesCFrame
					
					UIS.InputBegan:Connect(function(input)
						if input.KeyCode == Enum.KeyCode.R then
							rKeyIsPressed = true
							
							local rotationspeed = 5
							while rKeyIsPressed do
								wait()
								if PlacingStructure == true then
									yOrientation = yOrientation + rotationspeed
									cilentstructure.Orientation = Vector3.new(0,yOrientation,0)
								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.KeyCode == Enum.UserInputType.MouseButton1 then
							if PlacingStructure == true then
								if goodToPlace == true then
									local StructureCFrame = cilentstructure.CFrame
									PlacingStructure = PlaceStructure:InvokeServer(cilentstructure.Name, StructureCFrame)
									
									if placedStructure == true then
										PlacingStructure = false
										cilentstructure:Destroy()
										StructureFrame.Visible = true
									end
								end
							end
						end
					end)
				end)
			end
		end)
	end
end

Then Server Sided:

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

PlaceStructure.OnServerInvoke = function(player, StructureName, StructureCFrame)
	local crafted
	local realStructure = Structures:FindFirstChild(StructureName):Clone()
	
	if realStructure then
		realStructure.CFrame = StructureCFrame
		realStructure.Parent = workspace
		realStructure.Anchored = true
		crafted = true
	else
		crafted = false
	end
	
	return crafted
end

It’s too much work getting to know your code and finding out what exactly is going wrong, sorry :sweat_smile: But here’s how I would try to figure out what is going wrong:

You can put print statements around your code to see what code paths get executed: maybe an if statement that you think should be executed doesn’t actually. A good place to start is just before and just after the if PlaceStructure == false then. I’d also place one after where you clone the structure and set it up, to see if it really gets placed in Workspace. If that is the case, check the position of it. Maybe a calculation goes wrong that places it really far off in the distance so you can’t see it.

Another way to do essentially the same thing is with the debugger. It lets you place breakpoints and step through your code one statement at a time, so you can see exactly what happens. It also has a variable inspector so you can see the values of variables as they change.

Line 30 client side -

if (PlaceStructure == false) then -- did you mean to reference 'PlacingStructure' insteead?

Also, there’s a lot here in general - the first thing you should have done is place prints throughout the Client Side to see how far the code gets before it fails.

You also have two UIS.InputBegan events in your code - when you can simply moving the logic from the second into the first.

However, the biggest issue is that you’re putting these events inside a loop - if you do not move the events outside the RenderStepped loop - the client will create those events every frame (RenderStepped is 60fps) which will cause your game to lag very badly and potentially crash.

So

  • move your events outside of the RenderStepped loop
  • add in print code to see where your code does work
    then you will be able to narrow down what is causing the problem.
1 Like

where do you get this debugger? Is it a plugin or is it built in studio

there is a lua debugger built in (personally i couldn’t understand it) maybe there are video tutorials?

1 Like

Yeah there’s a tutorial here