Hello, I have this issue where i cant seem to find a answer to this.
I need my part to rotate when you need it to.
here is my script, parented to serverscriptservice.:
local placedObjects = workspace:WaitForChild("PlacedObjects")
local objects = RS:WaitForChild("Objects")
local function weld(currentObj)
for objs, obj in pairs(currentObj:GetDescendants()) do
if obj:IsA("BasePart") and obj ~= currentObj.PrimaryPart then
local weldConstraint = Instance.new("WeldConstraint")
weldConstraint.Part0 = obj
weldConstraint.Part1 = currentObj.PrimaryPart
weldConstraint.Parent = obj
obj.Anchored = false
obj.CanCollide = false
end
end
currentObj.PrimaryPart.CanCollide = false
return
end
local function init(obj)
local hitbox = Instance.new("Part")
local orientation, size = obj:GetBoundingBox()
hitbox.Size = size
hitbox.CFrame = orientation
hitbox.Transparency = 1
hitbox.Parent = obj
hitbox.Anchored = true
obj.PrimaryPart = hitbox
weld(obj)
return
end
local function place(obj, posX, posZ)
local orientationY = obj.PrimaryPart.Orientation.Y
obj:SetPrimaryPartCFrame(CFrame.new(posX, obj.PrimaryPart.Position.Y, posZ) * CFrame.Angles(0, math.rad(orientationY), 0))
obj.PrimaryPart.CanCollide = true
return
end
placeObject.OnServerEvent:Connect(function(plr, objectName, posX, posZ)
local foundObject = objects:FindFirstChild(objectName)
if foundObject then
local clonedObject = foundObject:Clone()
init(clonedObject)
place(clonedObject, posX, posZ)
clonedObject.Parent = placedObjects
end
end)
Here is my local script. Parented to startercharacterscript.
ocal CS = game:GetService("CollectionService")
local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")
local placeObject = RS:WaitForChild("PlaceObject")
local TI = TweenInfo.new(.4)
local objects = RS:WaitForChild("Objects")
local temporaryObjects = workspace:WaitForChild("TemporaryObjects")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local char = script.Parent
local currentObject = nil
local debounce = false
local function move()
local posX = math.floor(mouse.Hit.X)
local posZ = math.floor(mouse.Hit.Z)
local orientationY = currentObject.PrimaryPart.Orientation.Y
debounce = true
local Tween = TS:Create(currentObject.PrimaryPart, TI,
{CFrame = CFrame.new(posX, currentObject.PrimaryPart.Position.Y, posZ) * CFrame.Angles(0, math.rad(orientationY), 0)})
Tween:Play()
Tween.Completed:Wait()
debounce = false
end
local function weld()
debounce = true
for objs, obj in pairs(currentObject:GetDescendants()) do
if obj:IsA("BasePart") and obj ~= currentObject.PrimaryPart then
local weldConstraint = Instance.new("WeldConstraint")
weldConstraint.Part0 = obj
weldConstraint.Part1 = currentObject.PrimaryPart
weldConstraint.Parent = obj
obj.Anchored = false
obj.CanCollide = false
end
end
mouse.TargetFilter = currentObject
debounce = false
currentObject.PrimaryPart.CanCollide = false
return
end
local function init()
local hitbox = Instance.new("Part")
local orientation, size = currentObject:GetBoundingBox()
hitbox.Size = size
hitbox.CFrame = orientation
hitbox.Transparency = 1
hitbox.Parent = currentObject
hitbox.Anchored = true
currentObject.PrimaryPart = hitbox
weld()
return
end
local function place(tool)
placeObject:FireServer(currentObject.Name, currentObject.PrimaryPart.Position.X, currentObject.PrimaryPart.Position.Z)
temporaryObjects:ClearAllChildren()
tool:Destroy()
end
local function checkForEquipped()
for index, tool in pairs(CS:GetTagged("PlacementObject")) do
local moveConnection = nil
tool.Equipped:Connect(function()
local foundObject = objects:FindFirstChild(tool.Name)
if foundObject then
currentObject = foundObject:Clone()
currentObject.Parent = temporaryObjects
init()
moveConnection = mouse.Move:Connect(move)
move()
tool.Activated:Connect(function()
if moveConnection ~= nil then
moveConnection:Disconnect()
moveConnection = nil
place(tool)
end
end)
end
end)
tool.Unequipped:Connect(function()
if moveConnection ~= nil then
moveConnection:Disconnect()
moveConnection = nil
temporaryObjects:ClearAllChildren()
end
end)
end
end
checkForEquipped()