Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I animate an object that is welded using CFrame:Lerp()?

I'm trying to make the gun that is in the player's hand do a 360 rotation when i press q, but when I press q, the gun teleports all around the place here is my block of code

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Enabled = false

Mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "q" and Enabled == false then 
		Enabled = true
		
		lw = Player.Character.Welds.LeftArmWeld --This is the weld for the handle 
		lg = Player.Character.LeftGun.LeftHandle --The handle, everything is welded to this
		
		e1 = CFrame.Angles(math.rad(180),0,0)
		e2 = CFrame.Angles(math.rad(270),0,0)
		e3 = CFrame.Angles(math.rad(360),0,0)
		e4 = CFrame.Angles(math.rad(90),0,0)
		
		for i = 0,1,1 do 
			lw.C0 = lg.CFrame:lerp(e1, i)
			wait()
		end
		for i = 0,1,1 do 
			lw.C0 = lg.CFrame:lerp(e2, i)
			wait()
		end
		for i = 0,1,1 do 
			lw.C0 = lg.CFrame:lerp(e3, i)
			wait()
		end
		for i = 0,1,1 do 
			lw.C0 = lg.CFrame:lerp(e4, i)
			wait()
		end
		
		wait(1)
		Enabled = false		
	end
end)

1 comment

0

Mouse.KeyDown is deprecated! Deprecated deprecated deprecated! Why are you still using it!!? Seriously, dude.

1 answer

Try setting the weld's C0 and C1 properties instead of the handle's CFrame:

if key == "q" and Enabled == false then 
    Enabled = true

    lw = Player.Character.Welds.LeftArmWeld --This is the weld for the handle 
    lg = Player.Character.LeftGun.LeftHandle --The handle, everything is welded to this

    local originalC1 = lw.C1
    for i=1, 360, 1 do
           lw.C1 = originalC1 * CFrame.Angles(math.rad(i),0,0)
	   wait()
    end
    lw.C1 = originalC1

    wait(1)
    Enabled = false     
end

To change the rotation speed, change the second 1 in the for loop to another number.

Please tell me if it doesn't work, I can't access Studio right now.

2 comments

0

It works, but it's extremely slow, can u tell me how I can make the animation faster and explain what you did? That would help a lot. Thanks

0

What I did is I took the original rotation of the weld (lw.C1), stored it and made a loop which offsets that rotation by one degree each time. After that, I reset the rotation to the original one (just in case something went wrong). To change the speed, change the third number in the for loop to a greater number, like 2 or 3. For more info: http://wiki.roblox.com/index.php?title=Joints#C0_and_C1

This discussion is preserved as it was. New answers, comments, and votes are closed.