Scope Issue (Possibily?)

Hello, I am making a placement system, and I made a keybind to delete and rotate the part. At first, it works and all is well, but then once I delete it and make a new part, everything works but the rotating, and it throws an error: Players.BeardedNewbie.PlayerGui.BuildGui.Build.LocalScript:65: attempt to index nil with ‘Orientation’. Which means that the part doesn’t exist right? But it does. This is where I am stuck. I thought it might have to do with the scope but I really don’t know. All help is welcome. Code is below.

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

local player = Players.LocalPlayer
local mouse = player:GetMouse()
local Builds = ReplicatedStorage:WaitForChild("Builds")
local camera = workspace.CurrentCamera

local canPlace = true
local buildExists = false

local button = script.Parent
local block = Builds.Block

local plots = game.Workspace:WaitForChild("Plots")
local playerPlot

button.MouseButton1Up:Connect(function()
	for i, v in ipairs(plots:GetDescendants()) do
		if v:IsA("IntValue") and v.Name == "PlayerUser" then
			if v.Value == player.UserId then
				playerPlot = v.Parent
				break
			end
		end
	end
	
	buildExists = true
	local newBuild = block:Clone()
	newBuild.Parent = game.Workspace
	
	for i, v in ipairs(newBuild:GetDescendants()) do
		if v:IsA("BasePart") then
			v.Material = Enum.Material.ForceField
			v.BrickColor = BrickColor.new("Lime green")
		end
	end
	
	local connection = RunService.Stepped:Connect(function()
		local params = RaycastParams.new()
		params.FilterDescendantsInstances = {newBuild} 
		params.FilterType = Enum.RaycastFilterType.Blacklist 
		
		local loc = UserInputService:GetMouseLocation()
		local unitRay = camera:ScreenPointToRay(loc.X,loc.Y) 
		local result = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
		
		local s = newBuild:GetExtentsSize()
		
		if result then
			if result.Instance ~= newBuild then
				newBuild.PrimaryPart.Position = Vector3.new(math.floor(result.Position.X),result.Position.Y+ s.Y/2,math.floor(result.Position.Z))
			end
		end
	end)
	
	UserInputService.InputBegan:Connect(function(key, keyprocessed)
		if keyprocessed then return end

		if key.UserInputType == Enum.UserInputType.Keyboard then
			if buildExists then
				if key.KeyCode == Enum.KeyCode.R then
					newBuild.PrimaryPart.Orientation += Vector3.new(0,90,0)
				elseif key.KeyCode == Enum.KeyCode.Q then
					newBuild:Destroy()
					connection:Disconnect()
					buildExists = false
					return
				end
			end
		end
	end)	
end)


1 Like

bump ineedkeysforthissoheretheyare

i’m guessing after you delete the part you need to redefine the new part (the script still thinks that it needs to rotate the part that got deleted)

But the event runs every time I click, which means its defining a new part every time right?

here it defines it when the script initially runs

maybe this defines after you delete the first block?

I deleted the block in a UserInputService event, which should in theory exit out of the MouseButton1Up event right? And if it were to then I would’nt have this issue.

if possible try removing the “PrimaryPart”

It’s a model, so I can’t do this right?

you could also possibly just weld everything from the model into a single part and rotate that instead

I don’t think thats the issue. The issue is that the script is still trying to access the first part, rather than the newly defined part.

wait, have you already set the primary part of the model?

Yes sdfjklsdfjksdfjklsdfjklsdf

if you haven’t then just select the model you want and look for the “primary part” property

The rotating is not the problem, like I said, it works for the first time, but then after placing another build, rotating throws the error above.

so it may be because you only clone the block once, after its deleted the script doesn’t clone it again

It does clone it again though, I can see it, the script is just referencing the old part which was deleted

instead of destroying it then try just placing the cloned one back in rep storage, but change it’s name after cloning it

That would clutter repstorage tho, and make lag

you never dc the UserInputService.InputBegan:Connect(function(key, keyprocessed) event

I should put it to another connection?