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)