What the best way of creating a hit fling script?

Im trying to create a script that when you swing the hammer it flings you. I have everyhting set, but I am lost on where to start with the fling script. Whats the best way of doing this? Also can hitbox move with a animation? Thanks!

1 Like

To create a fling effect when swinging a hammer, you can use a BodyVelocity or BodyThrust object to apply a force to the player’s character when the hammer hits an object. Here’s an example script that applies a fling effect when the player hits a target with a hammer:

local tool = script.Parent
local hitbox = tool.Handle.Hitbox -- replace this with the actual name of your hitbox
local flingForce = 1000 -- adjust this to change the force of the fling

function onHit(target, hitPosition)
	-- apply a BodyVelocity to the player's character
	local character = tool.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local direction = (hitPosition - character.HumanoidRootPart.Position).Unit
		local velocity = direction * flingForce
		local bodyVelocity = Instance.new("BodyVelocity")
		bodyVelocity.Velocity = velocity
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.P = 10000
		bodyVelocity.Parent = humanoid.RootPart

		-- remove the BodyVelocity after a short delay
		wait(0.5)
		bodyVelocity:Destroy()
	end
end

-- connect the onHit function to the hit event of the hammer's hitbox
hitbox.Touched:Connect(function(part)
	local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid and humanoid.Health > 0 then
		onHit(humanoid, part.Position)
	end
end)

To move the hitbox with an animation, you can use the AnimationTrack object’s KeyframeReached event to detect when the animation reaches a certain point, and then set the position of the hitbox accordingly. Here’s an example script that moves the hitbox to a specific position when a certain keyframe is reached in the animation:

local tool = script.Parent
local handle = tool.Handle
local hitbox = handle.Hitbox -- replace this with the actual name of your hitbox
local animation = tool.Animation

function onKeyframeReached(keyframeName)
	if keyframeName == "MoveHitbox" then
		-- set the position of the hitbox to a specific offset from the handle
		hitbox.CFrame = handle.CFrame * CFrame.new(0, 0, -1)
	end
end

-- connect the onKeyframeReached function to the KeyframeReached event of the animation
local track = animation:FindFirstChildOfClass("AnimationTrack")
if track then
	track.KeyframeReached:Connect(onKeyframeReached)
end

In this example, the onKeyframeReached function is called when the animation reaches a keyframe named “MoveHitbox”. You can adjust the name and offset to match your animation. The function sets the position of the hitbox to an offset from the handle’s position using the CFrame property.

1 Like

Yoooo, Thanks this helped alot!

1 Like

How can I find the offset? To put it in Cframe!

1 Like

To find the offset, you can use the Vector3.new function to create a vector that represents the offset from the handle’s position. For example, if the hitbox should be moved 1 stud in the negative Z direction from the handle’s position, you can use Vector3.new(0, 0, -1). You can then use the CFrame.new function to create a CFrame that represents the offset, and multiply it by the handle’s CFrame to get the final position of the hitbox. Here’s an example:

local tool = script.Parent
local handle = tool.Handle
local hitbox = handle.Hitbox -- replace this with the actual name of your hitbox
local animation = tool.Animation

function onKeyframeReached(keyframeName)
	if keyframeName == "MoveHitbox" then
		-- set the position of the hitbox to a specific offset from the handle
		local offset = Vector3.new(0, 0, -1) -- replace with your desired offset
		hitbox.CFrame = handle.CFrame * CFrame.new(offset)
	end
end

-- connect the onKeyframeReached function to the KeyframeReached event of the animation
local track = animation:FindFirstChildOfClass("AnimationTrack")
if track then
	track.KeyframeReached:Connect(onKeyframeReached)
end

To find the offset, you can use the Vector3.new function to create a vector that represents the offset in x, y, and z directions. Then, you can use the CFrame.new function to create a CFrame that represents the offset, and multiply it by the handle’s CFrame to get the final position of the hitbox.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.