Object Pickup System

There is a bug where in my friends game any object that is picked up will be rapidly spinning. This began around when we tried to make a object maintain its orientation (such as in SCP-3008) However, this causes it to spin. All objects are welded beforehand.

Are there any solutions to fix this? Is there a way in order to perhaps optimize the code?

game:GetService("RunService").RenderStepped:Connect(function()
    if HoldingItem then
        local root = player.Character.HumanoidRootPart
        CurrentHolding.MoveRoot.CFrame = mouse.Origin * CFrame.new(0,0,-AwayDistance)
        CurrentHolding.MoveRoot.Orientation = Vector3.new(RotationX, RotationY, RotationZ)
        --CurrentHolding.MoveRoot.Orientation.Y = RotationY
    end
end)
2 Likes

Make sure the orientation values don’t increase or that you aren’t adding orientation to the object each time causing it to spin

It would be same as doing a loop where you do var = var + 1

(I’m the friends he’s talking about) Currently, all the rotations variables are set to 0.

Right here you can see How it looks if I Remove “CurrentHolding.MoveRoot.Orientation = Vector3.new(RotationX, RotationY, RotationZ)”"

Basically, what it does is make sure the MoveRoot has a normal rotation I don’t know if this is a problem tho?

So you are trying to keep the object at It’s original orientation rather than mouse given one right?

2 Likes

Yeah thats exactly what i mean. Im pretty bad at explaining that’s why

What would the AwayDistance be set as as a default?

The awaydistance is changed by scrolling. It basically tells the scripts how long away the object are.
I can send the entire script if you want?

You can if you’d like as I’m working with partial variables here

There sorry it took a while I’m new to Dev Forum.

local placing = false

local AwayDistance = 10 -- Can be changed by scrolling
local RotationX = 0
local RotationY = 0
local RotationZ = 0

game:GetService("RunService").RenderStepped:Connect(function()
	if HoldingItem then		
		CurrentHolding.MoveRoot.CFrame = mouse.Origin * CFrame.new(0,0,-AwayDistance)-- * CFrame.Angles(RotationX, RotationY, RotationZ)
		CurrentHolding.MoveRoot.Orientation = Vector3.new(RotationX, RotationY, RotationZ)
	end
end)

function Release(CurrentHolding)
	pickupEvent:FireServer(CurrentHolding, "Replace")
end

function pickup(PickupPart)
	HoldingItem = true
	print("picked up")
	CurrentHolding = PickupPart.Parent:Clone()
	CurrentHolding.Parent = workspace
	
	-- // make something idk
	local CurrentHoldingChildren = CurrentHolding:GetChildren()
	
	for i=1, #CurrentHoldingChildren do
		if CurrentHoldingChildren[i].ClassName == "Part" or CurrentHoldingChildren[i].ClassName == "MeshPart" then
			CurrentHoldingChildren[i].CanCollide = false
			CurrentHoldingChildren[i].Transparency = 0.5
		end
	end
	
	pickupEvent:FireServer(PickupPart, "Destroy")
end

input.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.Keyboard then
		if key.KeyCode == Enum.KeyCode.Q then
			if HoldingItem then
				HoldingItem = false
				Release(CurrentHolding)
			end
		end
	end
end)

mouse.WheelForward:Connect(function()
	if AwayDistance < 20 then
		AwayDistance = AwayDistance + 1
	end
end)

mouse.WheelBackward:Connect(function()
	if AwayDistance > 10 then
		AwayDistance = AwayDistance - 1
	end
end)

Preformatted text

game:GetService('RunService').RenderStepped:Connect(function()
	if HoldingItem then
		CurrentHolding.MoveRoot.CFrame = CFrame.new(player.Character.Head.Position + (((mouse.Hit.Position) - player.Character.Head.Position).Unit * AwayDistance))
	end
end)

You had the problem that you were using CFrame of mouse which includes all orientation and positioning of the mouse which rotated the whole part, what I did was created a vector between the head of the player (as you are in first person), this was out start position and our end position was the mouse.Hit which would be shortened down with the AwayDistance number.

From this I drew a basic vector for our direction and added it onto our start position giving us just the position change.

I believe that if you try to rotate it around using orientation or CFrame angles it shall work.

1 Like

So would this make the orientation stay Original?

Alight ill try thanks!

Yes, I suggest out of pure good practice you change object orientation in functions/code that are not linked to a loop to save on execution time that can lower your script’s performance.

Thank you so much this works! I would’ve never figured this out myself. Cant thank you enough!

1 Like