I have a bug and I dont know how to fix it?

Hello, so I made this CocaCola can and when the player presses ‘E’ I want the can to be in his hand, but the problem is I do not know how to orientation it to always be held like a normal can. Can someone help me heres my code:

local event = game.ReplicatedStorage.RemoteEvents.Drink

event.OnServerEvent:Connect(function(player)
print("event from the server")
local character = player.Character
local cocaColaNew = game.ReplicatedStorage.CocaColaCan:Clone()
cocaColaNew.Position = character:WaitForChild("Right Arm").Position + Vector3.new(0,-1,0)
cocaColaNew.Orientation = Vector3.new(90, -180, 0)
cocaColaNew.Parent = workspace

local weld = Instance.new("WeldConstraint")
weld.Part0 = cocaColaNew
weld.Part1 = character:WaitForChild("Right Arm")
weld.Parent = character:WaitForChild("Right Arm")

local weld2 = Instance.new("Motor6D")
weld.Part0 = cocaColaNew
weld.Part1 = character:WaitForChild("Right Arm")
weld.Parent = character:WaitForChild("Right Arm")

cocaColaNew.Anchored = false

end)

Here’s a video of the problem:

You could use CFrames for this and do:

cocaColaNew.CFrame = (character:WaitForChild("Right Arm").CFrame + character:WaitForChild("Right Arm").CFrame.UpVector * -1) * CFrame.Angles(math.rad(90), math.rad(-180), 0)
1 Like

Actually I fixed the problem but I still would appreciate if you could explain some of this I wanna improve as a scripter and some of these things like ‘angles, math.rad, and upVector’ caught me off guard.

1 Like

oK sO

First we assign the can’s cframe to the right arm’s cframe (cframe = both position and angle)

Then we use UpVector as Y axis shown in this article CFrame

After positioning the Can we will want to rotate it to our liking so we will use CFrame.Angles to adjust the rotation

math.rad returns the angle x, y (given in degrees) in radians

Sorry if it’s bad I don’t usually explain codes

1 Like

Thank you this helped and so did the article I will try to learn more about it but I only before just knew about CFrame from exploit coding thanks for your fast response and help! Vouch.

1 Like