I’m trying to make a road placement system. The road is a model.
Here is the code that matters:
RunService:BindToRenderStep("displayRoad", 1, function()
if mouse.Target == workspace.Baseplate or mouse.Target == newRoad then
local distance = (startingPoint - mouse.Hit.Position).magnitude
for i, v in pairs(newRoad:GetChildren()) do
v.Size = Vector3.new(v.Size.X, v.Size.Y, distance)
v.Orientation = Vector3.new(0, v.Orientation.Y, 0)
end
newRoad:SetPrimaryPartCFrame(CFrame.new(startingPoint, mouse.Hit.Position) * CFrame.new(0,0, -distance / 2))
end
end)
newRoad is the road model.
here is my entire code if you want to see it
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 character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local screenGui = script.Parent
local button = screenGui.TextButton
local label = screenGui.TextLabel
local road = ReplicatedStorage:WaitForChild("road", 5)
local choosingStart = false
local placingRoad = false
local remoteFunction = game.ReplicatedStorage:WaitForChild("RemoteFunction",3)
local maxPlaceDistance = 50
button.MouseButton1Click:Connect(function()
print("the button was clicked", choosingStart)
if choosingStart == false then
print("the button was clicked")
choosingStart = true
button.Visible = false
label.Visible = true
label.Text = "click where you want to start"
local newRoad = road:Clone()
UserInputService.InputBegan:Connect(function(input)
print("there is input")
if placingRoad == false and input.UserInputType == Enum.UserInputType.MouseButton1 and placingRoad == false then
print("player clicked")
placingRoad = true
local part = mouse.Target
local startingPoint = mouse.Hit.Position
if part and part == workspace.Baseplate and (startingPoint - humanoidRootPart.Position).magnitude < maxPlaceDistance then
newRoad.Parent = workspace
RunService:BindToRenderStep("displayRoad", 1, function()
if mouse.Target == workspace.Baseplate or mouse.Target == newRoad then
local distance = (startingPoint - mouse.Hit.Position).magnitude
for i, v in pairs(newRoad:GetChildren()) do
v.Size = Vector3.new(v.Size.X, v.Size.Y, distance)
v.Orientation = Vector3.new(0, v.Orientation.Y, 0)
end
newRoad:SetPrimaryPartCFrame(CFrame.new(startingPoint, mouse.Hit.Position) * CFrame.new(0,0, -distance / 2))
end
end)
end
elseif placingRoad and input.UserInputType == Enum.UserInputType.MouseButton1 then
local part = mouse.Target
local startingPoint = mouse.Hit.Position
if part and part == workspace.Baseplate and (startingPoint - humanoidRootPart.Position).magnitude < maxPlaceDistance then
placingRoad = false
local placed = remoteFunction:InvokeServer(newRoad.Size, newRoad.CFrame)
end
end
end)
end
end)
The code is in a localScript
Here is the original road model:
this is how I want the road to appear, but if I play, every part gets centralized. Here is what I mean:
robloxapp-20201121-2048130.wmv (403.8 KB)
I want the road to look like the original. I’m probably doing something wrong with
SetPrimaryPartCFrame()