How do I fix when my skill flinging me when I aim at the floor

I trying to make a skill system so I make a first skill that can hold press and aim at anywhere while you use it your roblox character will face to your mouse position and get freezed but the problem is when I aim at the floor my roblox character keep shaking and when I release skill button it just gonna fling my character
here is some of my code (I’m new for scripting so my script probably look messy sorry for that, you can comment and give me any information about scripting I will take your advice Thank you!)

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local HRP = chr:WaitForChild("HumanoidRootPart")
local hrpPos = HRP.CFrame.Position
local HUM = chr:FindFirstChildWhichIsA("Humanoid")
local Mouse = plr:GetMouse()
local mousePos = Mouse.Hit.Position
local UIS = game:GetService("UserInputService")
local DefaultWalkSpeed = 16
local DefaultJumpHeight = 7.2
local DB = game:GetService("Debris")

UIS.InputBegan:Connect(function(input, chating)
		if not chating then
			if input.KeyCode == Enum.KeyCode.Z then
				
				local BodyPosition = Instance.new("BodyPosition", HRP)
				BodyPosition.Name = "BodyPosition"
				BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
				BodyPosition.D = 0
				BodyPosition.P = 999999
				BodyPosition.Position = HRP.Position
				
				local BodyGyro= Instance.new("BodyGyro", HRP)
				BodyGyro.Name = "BodyGyro"
				BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
				BodyGyro.D = 100
				BodyGyro.P = 999999
				BodyGyro.CFrame = CFrame.lookAt(hrpPos, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
				
				HUM.WalkSpeed = 0
				HUM.JumpHeight = 0
				
				repeat
					mousePos = Mouse.Hit.Position
					BodyGyro.CFrame = CFrame.new(hrpPos, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
					wait()
				until HoldingSkill.Value == false
				
				DB:AddItem(BodyPosition, 0)
				DB:AddItem(BodyGyro, 0)

				HUM.WalkSpeed = DefaultWalkSpeed
				HUM.JumpHeight = DefaultJumpHeight
			end
		end
	end)

And here is my video about my trouble(sorry for lagging video my pc have low specs)
robloxapp-20221025-1846103.wmv (1.9 MB)

If anyone know how to fix this please comment I will take any advice Thank you so much!

I will start off by giving you some suggestions on some things to do.

  1. BodyPosition and BodyGyro and deprecated and there are some better alternatives
  2. Your repeat, until loop should be replaced with a RunService alternative (RS or HB)
  3. I would recommend using contextactionservice
  4. I wouldn’t really reccomend using Mouse because it can get a little inaccurate, but it will be fine here

Number one, let’s start by cleaning up the code a little bit:

local Players = game:GetService("Players")
local DB = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local plr = Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local HRP = chr:WaitForChild("HumanoidRootPart")
local HUM = chr:FindFirstChildWhichIsA("Humanoid")

local Mouse = plr:GetMouse()
local mousePos = Mouse.Hit.Position

local DefaultWalkSpeed = 16
local DefaultJumpHeight = 7.2

Much better. You may have realised that I removed your ‘hrpPos’ variable, it would make more sense to get the position inside the function, because you may have moved from the time you active this function.

To keep the player still, lets use AlignOrientation | Roblox Creator Documentation and AlignPosition | Roblox Creator Documentation.

Let’s create our instances:

			local alignPosition = Instance.new("AlignPosition")
			alignPosition.ApplyAtCenterOfMass = true
			alignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
			alignPosition.Attachment0 = HRP.RootRigAttachment
			alignPosition.Position = HRP.RootRigAttachment.WorldPosition
			alignPosition.Parent = HRP
			
			local alignOrientation = Instance.new("AlignOrientation")
			alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
			alignOrientation.Attachment0 = HRP.RootRigAttachment
			alignOrientation.Parent = HRP
			alignOrientation.MaxTorque = math.huge
			alignOrientation.MaxAngularVelocity = 50
			alignPosition.MaxVelocity = 200

You should also add platform stand so the character is wiggling around ridiculously in the air:

HUM:ChangeState(Enum.HumanoidStateType.PlatformStanding)

Let’s add our better loop

	while HoldingSkill == false do
				mousePos = Mouse.Hit.Position
				alignOrientation.CFrame = CFrame.new(HRP.RootRigAttachment.WorldPosition, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
				RunService.Heartbeat:Wait()
			end

And let’s just clear things up:


			DB:AddItem(alignPosition, 0)
			DB:AddItem(alignOrientation, 0)

			HUM.WalkSpeed = DefaultWalkSpeed
			HUM.JumpHeight = DefaultJumpHeight
			
			HUM:ChangeState(Enum.HumanoidStateType.GettingUp)

The reason why we use the GettingUp state is so that the player doesn’t flop on the floor after the ability is over.

https://i.gyazo.com/43852d2648be98649bf835e430a094b8.mp4

Proof:

And here is your final code:

local Players = game:GetService("Players")
local DB = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local plr = Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local HRP = chr:WaitForChild("HumanoidRootPart")
local HUM = chr:FindFirstChildWhichIsA("Humanoid")

local Mouse = plr:GetMouse()
local mousePos = Mouse.Hit.Position

local DefaultWalkSpeed = 16
local DefaultJumpHeight = 7.2

UIS.InputBegan:Connect(function(input, chating)
	if not chating then
		if input.KeyCode == Enum.KeyCode.Z then
			
			local alignPosition = Instance.new("AlignPosition")
			alignPosition.ApplyAtCenterOfMass = true
			alignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
			alignPosition.Attachment0 = HRP.RootRigAttachment
			alignPosition.Position = HRP.RootRigAttachment.WorldPosition
			alignPosition.Parent = HRP
			
			local alignOrientation = Instance.new("AlignOrientation")
			alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
			alignOrientation.Attachment0 = HRP.RootRigAttachment
			alignOrientation.Parent = HRP
			alignOrientation.MaxTorque = math.huge
			alignOrientation.MaxAngularVelocity = 50
			alignPosition.MaxVelocity = 200
			
			HUM.WalkSpeed = 0
			HUM.JumpHeight = 0
			
			HUM:ChangeState(Enum.HumanoidStateType.PlatformStanding)
			
			while HoldingSkill.Value == false do
				mousePos = Mouse.Hit.Position
				alignOrientation.CFrame = CFrame.new(HRP.RootRigAttachment.WorldPosition, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
				RunService.Heartbeat:Wait()
			end
			
			DB:AddItem(alignPosition, 0)
			DB:AddItem(alignOrientation, 0)

			HUM.WalkSpeed = DefaultWalkSpeed
			HUM.JumpHeight = DefaultJumpHeight
			
			HUM:ChangeState(Enum.HumanoidStateType.GettingUp)
		end
	end
end)


If you have any questions please do respond!

3 Likes

Thank you so much man that helped me a lot for scripting I never know there is better version of BodyPosition and BodyGyro.

but I don’t understand some of your code it will be very good if you explain some of them

1.What is RootRigAttachment and WorldPosition I never see that code before is it different from Position?

2.You said that Mouse can get a little inaccurate is there anything better than using Mouse?

3.I making animation script and I want my skill holdable so I devided it to 3 parts 3.1) startpart 3.2) holdpart 3.3) endpart

Here is my acutually full code for now(I add your code too it’s work very perfectly tysm)

local Players = game:GetService("Players")
local DB = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")

local plr = Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local HRP = chr:WaitForChild("HumanoidRootPart")
local HUM = chr:FindFirstChildWhichIsA("Humanoid")
local ShootingSkill = plr.Backpack.ShootingSkill
local shootingSkillGui = plr.PlayerGui.ShootingSkillGui
local Folder = RS[plr.Name.. "Data"]
local HoldingSkill = Folder.holdingSkill
local skillZCooldown = Folder.shootingZCooldown
local startSkill = RS.ShootingSkillZAnim
local holdSkill = RS.HoldingSkillZAnim
local endSkill = RS.EndingSkillZAnim
local animator = HUM:FindFirstChildWhichIsA("Animator")
local loadStartAnimDone = true
local loadEndAnimDone = true

local Mouse = plr:GetMouse()
local mousePos = Mouse.Hit.Position

local Equiping = false
local DefaultWalkSpeed = 16
local DefaultJumpHeight = 7.2

local loadStartAnim = animator:LoadAnimation(startSkill)
local loadHoldAnim = animator:LoadAnimation(holdSkill)
local loadEndAnim = animator:LoadAnimation(endSkill)

UIS.InputBegan:Connect(function(input, chating)
	if not chating then
		if input.KeyCode == Enum.KeyCode.Z and skillZCooldown.Value == 0 then
			
			local holdAnim = coroutine.wrap(function()
				loadStartAnimDone = false
				loadStartAnim:Play()

				wait(0.48)
				
				loadStartAnimDone = true
				loadStartAnim:Stop()
				loadHoldAnim:Play()
			end)

			local endAnim = coroutine.wrap(function()
				loadEndAnimDone = false
				loadHoldAnim:Stop()
				loadEndAnim:Play()

				wait(0.12)
				
				loadEndAnimDone = true
				loadEndAnim:Stop()
			end)
			
			local alignPosition = Instance.new("AlignPosition")
			alignPosition.ApplyAtCenterOfMass = true
			alignPosition.RigidityEnabled = true
			alignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
			alignPosition.Attachment0 = HRP.RootRigAttachment
			alignPosition.Position = HRP.RootRigAttachment.WorldPosition
			alignPosition.Parent = HRP

			local alignOrientation = Instance.new("AlignOrientation")
			alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
			alignOrientation.Attachment0 = HRP.RootRigAttachment
			alignOrientation.Parent = HRP
			alignOrientation.RigidityEnabled = true
			
			HoldingSkill.Value = true
			HUM.WalkSpeed = 0
			HUM.JumpHeight = 0
			skillZCooldown.Value = 3
			holdAnim()

			HUM:ChangeState(Enum.HumanoidStateType.PlatformStanding)

			while HoldingSkill.Value do
				mousePos = Mouse.Hit.Position
				alignOrientation.CFrame = CFrame.new(HRP.RootRigAttachment.WorldPosition, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
				RunService.Heartbeat:Wait()
			end
			
			while not loadStartAnimDone do
				RunService.Heartbeat:Wait()
			end
			
			
			DB:AddItem(alignPosition, 0)
			DB:AddItem(alignOrientation, 0)
			endAnim()

			HUM.WalkSpeed = DefaultWalkSpeed
			HUM.JumpHeight = DefaultJumpHeight

			HUM:ChangeState(Enum.HumanoidStateType.GettingUp)
		end
	end
end)

UIS.InputEnded:Connect(function(input, chating)
	if not chating then
		if input.KeyCode == Enum.KeyCode.Z then
			HoldingSkill.Value = false
		end
	end
end)

ShootingSkill.Equipped:Connect(function()
	shootingSkillGui.Enabled = true
	Equiping = true
end)

ShootingSkill.Unequipped:Connect(function()
	shootingSkillGui.Enabled = false
	Equiping = false
end)

I use animator object in Humanoid for play my animation.

I use coroutine function for my animation cuz I don’t know how to run animation and script at the same time in better way.

I add 1 more while loop cuz I want to make player character stay at the same position but I don’t want them to turn around so I add while loop that will wait until animator playing animation done.

I wanna ask you is that my script normally or it’s messy you can tell me everything to make my script look better or make it orderly I will try my best to fix it. Thank you again!

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