How can I make a model shake when you hit it?

Hello , I’m currently working on a battle royale game and i want to make parts shake when you hit them with your pickaxe (just like in strucid).
I tried moving it by using CFrame but that was unsmooth. How can I make it shake smoothly? :smile: Any help would be very appreciated!

4 Likes

Tweening is a good option and allows for a smoother movement.

Another choice is animating the model, basically parent the model to a hitbox brick which moves the entire model. Then animate the hitbox as a shaky movement.

Edit: Or make completely custom animations for all the things that can be destroyed. (idk but may lower load times and make it more laggy)

1 Like

If you ever want to move parts smoothly in your game, just use TweenService, like @3rdhoan123 said.

e.g.:

TweenService = game:GetService("TweenService")

function ShakePart(part)
	local cycleDuration = 0.1
	local totalDuration = 0.5
	local volatility = 1
	
	local savedPosition = part.Position
	local tweeninfo = TweenInfo.new(
		cycleDuration,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)


	for i = 0, totalDuration - cycleDuration, cycleDuration do
		local tween = TweenService:Create(
			part,
			tweeninfo,
			{Position = savedPosition + Vector3.new(math.random(),math.random(),math.random()).Unit * volatility}
		)
		tween:Play()
		tween.Completed:Wait()
	end
	TweenService:Create(
		part,
		tweeninfo,
		{Position = savedPosition}
	):Play()
end

You might also want to play a sound somewhere at the beginning for more pizzazz, but that’s up to you.

10 Likes

There seems to be an error on the 31 , 36 lines;
Screenshot_2

1 Like

Could we see the code you’re using to make contact with the model to animate it?
That would help with finding the issue.

1 Like

I added a ‘,’ at the 31th line and now it seems to be working.
Here is the code:

TweenService = game:GetService("TweenService")

function ShakePart(part)
	local cycleDuration = 0.1
	local totalDuration = 0.5
	local volatility = 1
	
	local savedPosition = part.Position
	local tweeninfo = TweenInfo.new(
		cycleDuration,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)


	for i = 0, totalDuration - cycleDuration, cycleDuration do
		local tween = TweenService:Create(
			part,
			tweeninfo,
			{Position = savedPosition + Vector3.new(math.random(),math.random(),math.random()).Unit * volatility}
		)
		tween:Play()
		tween.Completed:Wait()
	end
	TweenService:Create(
		part,
		tweeninfo,
		{Position = savedPosition}
	):Play()
end
while true do
	wait(0.5)
	ShakePart(script.Parent)
end
1 Like

Oh, nvm. If @goldenstein64’s tip worked, you should mark it as a solution :smiley:

Already done it. :wink: Ty for trying to help me tho

Your welcome, glad to help! Good luck on your project btw :stuck_out_tongue:

2 Likes