(I have no idea if this is some error from me not realizing something in the code.)
Alright, So I was working on a building/placement system for one of my games, but I’ve run into an issue.
Everything was working, until when testing I realized that my placement menu won’t show up after placing an item.
I’ve tried many different solutions, no errors in the output whatsoever.
Now, I can usually fix these types of things, but this I just cannot figure this out
Code:
--keep in mind some of this is from a tutorial, I'm still learning :)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceObject = ReplicatedStorage:WaitForChild("PlaceItem")
local Objects = ReplicatedStorage:WaitForChild("Objects")
local ScaleFrame = script.Parent.ScaleFrame
local AwaitingPlacement = true -- I don't know if I really need this, but it's here for now.
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local ObjectFrame = script.Parent.ItemFrame1
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 1
local maxPlacingDistance = 20
local rKeyIsPressed = false
local placingObject = false
for _, ObjectButton in pairs(ObjectFrame:GetChildren()) do
if ObjectButton:IsA("TextButton") then
ObjectButton.MouseButton1Click:Connect(function()
ObjectFrame.Visible = false
local yOrientation = 0
local zOrientation = 0
local goodToPlace = false
local placedObject
if placingObject == false then
placingObject = true
local clientObject = Objects:FindFirstChild(ObjectButton.Name):Clone()
clientObject.BrickColor = BrickColor.new("Forest green")
clientObject.Material = Enum.Material.Neon
clientObject.CanCollide = false
clientObject.Parent = game.Workspace
ScaleFrame.Current.Text = clientObject.Size.X
local startingCFrame = CFrame.new(0,-2,-15)
clientObject.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
RunService.RenderStepped:Connect(function()
local mouseRay = mouse.UnitRay
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
local ignoreList = {clientObject, char}
local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
if hit and (HumanoidRootPart.Position - clientObject.Position).Magnitude < maxPlacingDistance then
goodToPlace = true
clientObject.BrickColor = BrickColor.new("Forest green")
else
goodToPlace = false
clientObject.BrickColor = BrickColor.new("Crimson")
end
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
local newCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
clientObject.CFrame = newCFrame * newAnglesCFrame
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R or input.KeyCode == Enum.KeyCode.T then -- T will be used for other axis rotating.
rKeyIsPressed = true
local rotationSpeed = 5
while rKeyIsPressed do
wait()
if placingObject == 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 placingObject == true then
if goodToPlace == true and AwaitingPlacement then
AwaitingPlacement = false
local ObjectCFrame = clientObject.CFrame
placedObject = PlaceObject:InvokeServer(clientObject.Name, ObjectCFrame)
wait()
AwaitingPlacement = true
if placedObject then
placingObject = false
clientObject:Destroy()
ObjectFrame.Visible = true
end
end
end
end
end)
end
end)
end
end
Yes, I know the code is pretty bad, I’ll probably remove the code once I get a solution.