I made a pretty basic placement script for models and it works as intended however on the second placement is blows up the output with errors, there are 2 different errors that show up ( output pics bellow) My guess is that it’s for some reason still trying to get the data from the last model but yet it still runs without stopping. any idea how to fix this?
local replicated = game:GetService("ReplicatedStorage")
local structures = replicated:WaitForChild("Structures")
local PlaceSctructure = replicated.Events:WaitForChild("PlaceStructure")
local UIS = game:GetService("UserInputService")
local runserv = game:GetService("RunService")
local player = game.Players.LocalPlayer
local StructureFrame = script.Parent
local char = player.Character or player.Character:WaitForChild()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 5
local MaxPlacement = 50
local rKeyisPressed = false
local placingStructure = false
for _, menuButton in pairs(StructureFrame:GetChildren()) do
if menuButton:IsA("TextButton") then
menuButton.MouseButton1Click:Connect(function()
StructureFrame.Visible = false
local yOrientation = 0
local goodtoplace = false
local placedstructure
if placingStructure == false then
placingStructure = true
local Clientstructure = structures:FindFirstChild(menuButton.Name):Clone()
Clientstructure.Parent = game.Workspace
local startingCFrame = CFrame.new(0, -2, -15)
Clientstructure:SetPrimaryPartCFrame(HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame))
runserv.RenderStepped:Connect(function()
if placingStructure == true then
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("Terrain") or hit.Name:lower() == "terrain") and (HumanoidRootPart.Position - Clientstructure.PrimaryPart.Position).Magnitude < MaxPlacement then
goodtoplace = true
else
goodtoplace = false
end
local newAngleCframe = CFrame.Angles(0, math.rad(yOrientation), 0)
local newCframe = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
Clientstructure:SetPrimaryPartCFrame(newCframe * newAngleCframe)
end
end)
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
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
print(goodtoplace)
if placingStructure == true then
if goodtoplace == true then
local structureCframe = Clientstructure.PrimaryPart.CFrame
placedstructure = PlaceSctructure:InvokeServer(Clientstructure.Name, structureCframe)
if placedstructure == true then
placingStructure = false
Clientstructure:Destroy()
StructureFrame.Visible = true
end
end
end
end
end)
end
end)
end
end