You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I want to figure out what is the problem is with my code and why it is not working
-
What is the issue?
I don’t know the problem with my code
-
What solutions have you tried so far?
Proofreading my code if there is anything causing the issue
I am trying to make a placement system for my Roblox game. It is expected to show a client-sided preview of the model that it is supposed to place, then it will place it.
The problem is that the model doesn’t clone at all for some reason even with commands that will instruct it to clone it.
Here is the script, client, and server-sided
Client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage.Structures
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local StructureFrame = script.Parent.ScrollingFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local Mouse = player:GetMouse()
local yBuildingOffset = 1
local MaxBuildingdistance = math.huge
local zKeyIsPressed = false
local PlacingStructure = false
local yOrientation = 0
local rKeyIsPressed = false
for _, structurebutton in pairs(StructureFrame:GetChildren()) do
if structurebutton:IsA("TextButton") then
structurebutton.MouseButton1Up:Connect(function()
StructureFrame.Visible = false
local yOrientation = 0
local goodToPlace = false
local placedStructure = false
if PlaceStructure == false then
PlacingStructure = true
local cilentstructure = Structures:FindFirstChild(structurebutton.Name):Clone()
cilentstructure.Brickcolor = BrickColor.new("Forest green")
cilentstructure.Material = "Neon"
cilentstructure.CanCollide = false
cilentstructure.Parent = game.Workspace
local StartingCFrame = CFrame.new(0, -2, - 15)
cilentstructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingCFrame)
RunService.RenderStepped:Connect(function()
local mouseRay = Mouse.UnitRay
local castray = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
local ignorelist = {cilentstructure,char}
local hit, position = workspace:FindPartsOnRayWithIgnoreList(castray, ignorelist)
if hit then
goodToPlace = true
cilentstructure.BrickColor = BrickColor.new("Forest green")
else
goodToPlace = false
cilentstructure.BrickColor = BrickColor.new("Crimson")
end
local newanglesCFrame = CFrame.new(0, math.rad(yOrientation), 0)
local newCFrame = CFrame.new(position.X,position.Y + yBuildingOffset, position.Z)
cilentstructure.CFrame = newCFrame * newanglesCFrame
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
cilentstructure.Orientation = Vector3.new(0,yOrientation,0)
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.KeyCode == Enum.UserInputType.MouseButton1 then
if PlacingStructure == true then
if goodToPlace == true then
local StructureCFrame = cilentstructure.CFrame
PlacingStructure = PlaceStructure:InvokeServer(cilentstructure.Name, StructureCFrame)
if placedStructure == true then
PlacingStructure = false
cilentstructure:Destroy()
StructureFrame.Visible = true
end
end
end
end
end)
end)
end
end)
end
end
Then Server Sided:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")
PlaceStructure.OnServerInvoke = function(player, StructureName, StructureCFrame)
local crafted
local realStructure = Structures:FindFirstChild(StructureName):Clone()
if realStructure then
realStructure.CFrame = StructureCFrame
realStructure.Parent = workspace
realStructure.Anchored = true
crafted = true
else
crafted = false
end
return crafted
end