-
What do you want to achieve? Keep it simple and clear!
I want to fix my snap building system like rust. -
What is the issue? Include screenshots / videos if possible!
The script is not detecting the attachments or anything that I set for snapping. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have followed this devforum (Creating a Rust Building System with Attachments and Raycasting!) but I don’t know if it is outdated or something since the code is still not working.
Here is the script I modified trying to make it work.
local plrs = game:GetService("Players")
local rs = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local plr = plrs.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local frame = script.Parent.Frame
local function unitRay()
local ur = mouse.UnitRay
return ur.Origin,ur.Direction * 10000
end
local object = rs:WaitForChild("ClientStructures"):WaitForChild(script.Parent.CurrentStructure.Value)
local clonedObject = object:Clone()
clonedObject.Parent = workspace
clonedObject.Material = Enum.Material.ForceField
clonedObject.Color = Color3.fromRGB(0,255,0)
clonedObject.Anchored = true
clonedObject.CanCollide = false
local GoodToPlace = false
local Attachment = nil
local enabled = script.Parent.BuildEnabled
local firstbuild = true
runService.RenderStepped:Connect(function()
if enabled.Value == true then
if clonedObject.Transparency == 1 then
clonedObject.Transparency = 0
end
local Origin, Direction = unitRay()
local Params = RaycastParams.new()
if object.Name ~= script.Parent.CurrentStructure.Value then
clonedObject:Destroy()
object = rs:WaitForChild("ClientStructures"):WaitForChild(script.Parent.CurrentStructure.Value)
clonedObject = object:Clone()
clonedObject.Parent = workspace
clonedObject.Material = Enum.Material.ForceField
clonedObject.Color = Color3.fromRGB(0,255,0)
clonedObject.Anchored = true
clonedObject.CanCollide = false
end
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = {char, clonedObject}
local rayCast = workspace:Raycast(Origin, Direction, Params)
local target = mouse.Target
if rayCast and rayCast.Instance then
local raycastInstace = rayCast.Instance
local nearestAttachment = nil
if target then
if target:FindFirstChild(clonedObject.Name) then
local attachPosition = target.Position
local attachOrientation = target.Orientation
local raycastPosition = rayCast.Position
nearestAttachment = target
Attachment = target
local cframe = target.CFrame
cframe = cframe *CFrame.Angles(0, math.rad(target.Orientation.Y), 0)
if GoodToPlace ~= nil then
GoodToPlace = true
clonedObject.Color = Color3.fromRGB(0,255,0)
clonedObject.CFrame = cframe
end
end
end
if nearestAttachment == nil and GoodToPlace ~= nil then
if firstbuild == false then
clonedObject.CFrame = CFrame.new(rayCast.Position.X, rayCast.Position.Y + clonedObject.Size.Y/2, rayCast.Position.Z)
GoodToPlace = false
clonedObject.Color = Color3.fromRGB(255,0,0)
clonedObject.Transparency = 0
elseif firstbuild == true then
clonedObject.CFrame = CFrame.new(rayCast.Position.X, rayCast.Position.Y + clonedObject.Size.Y/2, rayCast.Position.Z)
GoodToPlace = true
clonedObject.Color = Color3.fromRGB(0,255,0)
clonedObject.Transparency = 0
end
end
end
end
end)
uis.InputBegan:Connect(function(Input, gameProcessedEvent)
if gameProcessedEvent then return end
if enabled.Value == true then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if GoodToPlace then
if Attachment ~= nil then
Attachment:SetAttribute("Placed", true)
rs:WaitForChild("PlaceStructure"):InvokeServer(clonedObject.Name, clonedObject.CFrame)
elseif firstbuild == true then
firstbuild = false
local buildValue = Instance.new("BoolValue")
buildValue.Name = "FirstBuild"
buildValue.Parent = clonedObject
rs:WaitForChild("PlaceStructure"):InvokeServer(clonedObject.Name, clonedObject.CFrame)
end
end
elseif Input.UserInputType == Enum.UserInputType.MouseButton2 then
enabled.Value = false
clonedObject.Transparency = 1
script.Parent.ResetGUI.Value = true
end
end
end)