So, like the title says, I need some help with being able to clean up
This code
local function CrouchOrGP(BindName:string, UIS:Enum.UserInputState, Input:InputObject)
local Idle = Animator:LoadAnimation(AdvancedAnimations:FindFirstChild("crouchIdle"))
local Walk = Animator:LoadAnimation(AdvancedAnimations:FindFirstChild("crouchWalk"))
Walk.Priority = Enum.AnimationPriority.Action2
Idle.Priority = Enum.AnimationPriority.Action
if Input.UserInputType == Enum.UserInputType.Keyboard or Input.UserInputType == Enum.UserInputType.Gamepad1 then
if UIS == Enum.UserInputState.Begin then
local currState = Humanoid:GetState()
local CrouchAnimations = {
idle = AdvancedAnimations.crouchIdle,
walk = AdvancedAnimations.crouchWalk
}
ContextActionService:UnbindAction("Sprinting")
canCarry = false
if canGP and (currState == Enum.HumanoidStateType.Freefall or currState == Enum.HumanoidStateType.Jumping) then
local GPAnimation = Animator:LoadAnimation(AdvancedAnimations.groundPound)
ContextActionService:UnbindAction("CrouchOrGP")
canGP = false
print("ground pound")
local gpHitbox = Instance.new("Part")
local gpWeld = Instance.new("WeldConstraint")
gpHitbox.Size = Head.Size * 1.5
gpHitbox.Name = "hitBox"
gpHitbox.Transparency = 1
gpHitbox.LocalTransparencyModifier = 0.5
gpHitbox.Position = Head.Position
gpHitbox.Massless = true
gpHitbox.CanCollide = false
gpHitbox.Parent = Character
gpWeld.Part0 = Head
gpWeld.Part1 = gpHitbox
gpWeld.Name = "hitboxWeld"
gpWeld.Parent = gpHitbox
HumanoidRootPart.Anchored = true
GPAnimation:Play(0, 1, 1.4)
GPAnimation.Priority = Enum.AnimationPriority.Action2
ContextActionService:BindActionAtPriority("DisableControls", DisableControls, false, Enum.ContextActionPriority.High.Value, unpack(Enum.PlayerActions:GetEnumItems()))
if StarterGui:FindFirstChild("TouchGui") then StarterGui.TouchGui.Enabled = false end
for _, track:AnimationTrack in ipairs(Animator:GetPlayingAnimationTracks()) do
if track.Name:lower() == "doublejump" then
track:Stop(0)
end
end
onGPConnection = GPAnimation:GetMarkerReachedSignal("unanchor"):Connect(function()
HumanoidRootPart.Anchored = false
local trail = Instance.new("Trail")
local keypoints = {
Transparency = {
NumberSequenceKeypoint.new(0, 0.306, 0),
NumberSequenceKeypoint.new(0.621, 0.481, 0.5),
NumberSequenceKeypoint.new(1, 1, 0),
},
WidthScale = {
NumberSequenceKeypoint.new(0, 1, 0),
NumberSequenceKeypoint.new(0.644, 0.869, 0.5),
NumberSequenceKeypoint.new(1, 0, 0),
}
}
trail.Attachment0 = Head:WaitForChild("FaceFrontAttachment")
trail.Attachment1 = Head:WaitForChild("HairAttachment")
trail.Transparency = NumberSequence.new(keypoints.Transparency)
trail.WidthScale = NumberSequence.new(keypoints.WidthScale)
trail.Lifetime = 0.2
trail.LightEmission = 0.5
trail.LightInfluence = 1
trail.Parent = Head
task.wait(0.1)
GPAnimation:AdjustSpeed(0)
gpHitbox.Touched:Connect(function(hitPart)
if hitPart.Anchored then
onGPConnection:Disconnect()
gpHitbox:Destroy()
local smoke, stars = Effects:WaitForChild("Smoke"):Clone(), Effects:WaitForChild("Stars"):Clone()
smoke.Parent = ParticlePoints.gp
stars.Parent = ParticlePoints.gp
smoke:Emit(math.random(7, 12))
stars:Emit(math.random(3, 6))
HumanoidRootPart.Anchored = true
if hitPart.Name:find("button") and hitPart.Parent:IsA("Model") and hitPart.Parent:GetAttribute("Type") == "button" then
Remotes:WaitForChild("ButtonPressed"):FireServer(hitPart, hitPart.Parent)
end
task.wait(0.15)
canCarry = true
ContextActionService:BindAction("CrouchOrGP", CrouchOrGP, true, Enum.KeyCode.LeftControl)
ContextActionService:BindAction("Sprinting", Sprinting, true, Enum.KeyCode.LeftShift)
ContextActionService:UnbindAction("DisableControls")
if StarterGui:FindFirstChild("TouchGui") then StarterGui.TouchGui.Enabled = true end
GPAnimation:Stop(0)
HumanoidRootPart.Anchored = false
trail:Destroy()
end
end)
end)
elseif currState == Enum.HumanoidStateType.Running then
Idle:Play(0.5, 2, 1 / (Humanoid.Health / 100))
onCrouchConnection = Humanoid.Running:Connect(function(speed)
if speed > 0.01 then
Walk:Play(0.4, 2, (Humanoid.WalkSpeed / 7) / 2)
Idle:Stop(0.4)
else
Walk:Stop(0.5)
Idle:Play(0.5, 2, 1 / (Humanoid.Health / 100))
end
end)
print("crouching")
Humanoid.JumpPower = JPTypes.crouch
Humanoid.WalkSpeed = math.round(WSTypes.crouch)
canDJ = false
ContextActionService:UnbindAction("Sprinting")
end
elseif UIS == Enum.UserInputState.End then
print("no longer crouching")
Humanoid.JumpPower = JPTypes.norm
Humanoid.WalkSpeed = WSTypes.norm
canCarry = true
if onCrouchConnection then
onCrouchConnection:Disconnect()
end
for _, track:AnimationTrack in ipairs(Animator:GetPlayingAnimationTracks()) do
if track.Name:lower() == "crouchidle" or track.Name:lower() == "crouchwalk" then
track:Stop(0.7)
end
end
ContextActionService:BindAction("CrouchOrGP", CrouchOrGP, true, Enum.KeyCode.LeftControl)
ContextActionService:BindAction("Sprinting", Sprinting, true, Enum.KeyCode.LeftShift)
end
else
if UIS == Enum.UserInputState.End then
if not crouching then
local currState = Humanoid:GetState()
local CrouchAnimations = {
idle = AdvancedAnimations.crouchIdle,
walk = AdvancedAnimations.crouchWalk
}
ContextActionService:UnbindAction("Sprinting")
canCarry = false
if canGP and (currState == Enum.HumanoidStateType.Freefall or currState == Enum.HumanoidStateType.Jumping) then
local GPAnimation = Animator:LoadAnimation(AdvancedAnimations.groundPound)
ContextActionService:UnbindAction("CrouchOrGP")
canGP = false
print("ground pound")
local gpHitbox = Instance.new("Part")
local gpWeld = Instance.new("WeldConstraint")
gpHitbox.Size = Head.Size * 1.5
gpHitbox.Name = "hitBox"
gpHitbox.Transparency = 1
gpHitbox.LocalTransparencyModifier = 0.5
gpHitbox.Position = Head.Position
gpHitbox.Massless = true
gpHitbox.CanCollide = false
gpHitbox.Parent = Character
gpWeld.Part0 = Head
gpWeld.Part1 = gpHitbox
gpWeld.Name = "hitboxWeld"
gpWeld.Parent = gpHitbox
HumanoidRootPart.Anchored = true
GPAnimation:Play(0, 1, 1.6)
GPAnimation.Priority = Enum.AnimationPriority.Action2
ContextActionService:BindActionAtPriority("DisableControls", DisableControls, false, Enum.ContextActionPriority.High.Value, unpack(Enum.PlayerActions:GetEnumItems()))
if StarterGui:FindFirstChild("TouchGui") then StarterGui.TouchGui.Enabled = false end
for _, track:AnimationTrack in ipairs(Animator:GetPlayingAnimationTracks()) do
if track.Name:lower() == "doublejump" then
track:Stop(0)
end
end
onGPConnection = GPAnimation:GetMarkerReachedSignal("unanchor"):Connect(function()
HumanoidRootPart.Anchored = false
HumanoidRootPart.AssemblyLinearVelocity = Vector3.new(0, -5, 0)
local trail = Instance.new("Trail")
local keypoints = {
Transparency = {
NumberSequenceKeypoint.new(0, 0.306, 0),
NumberSequenceKeypoint.new(0.621, 0.481, 0.5),
NumberSequenceKeypoint.new(1, 1, 0),
},
WidthScale = {
NumberSequenceKeypoint.new(0, 1, 0),
NumberSequenceKeypoint.new(0.644, 0.869, 0.5),
NumberSequenceKeypoint.new(1, 0, 0),
}
}
trail.Attachment0 = Head:WaitForChild("FaceFrontAttachment")
trail.Attachment1 = Head:WaitForChild("HairAttachment")
trail.Transparency = NumberSequence.new(keypoints.Transparency)
trail.WidthScale = NumberSequence.new(keypoints.WidthScale)
trail.Lifetime = 0.2
trail.LightEmission = 0.5
trail.LightInfluence = 1
trail.Parent = Head
task.wait(0.1)
GPAnimation:AdjustSpeed(0)
gpHitbox.Touched:Connect(function(hitPart)
print(hitPart)
if hitPart.CollisionGroupId >= 3 then
onGPConnection:Disconnect()
gpHitbox:Destroy()
local smoke, stars = Effects:WaitForChild("Smoke"):Clone(), Effects:WaitForChild("Stars"):Clone()
smoke.Parent = ParticlePoints.gp
stars.Parent = ParticlePoints.gp
smoke:Emit(math.random(7, 12))
stars:Emit(math.random(3, 6))
HumanoidRootPart.Anchored = true
if hitPart.Name:find("button") and hitPart.Parent:IsA("Model") and hitPart.Parent:GetAttribute("Type") == "button" then
Remotes:WaitForChild("ButtonPressed"):FireServer(hitPart, hitPart.Parent)
end
task.wait(0.15)
ContextActionService:BindAction("CrouchOrGP", CrouchOrGP, true, Enum.KeyCode.LeftControl)
ContextActionService:BindAction("Sprinting", Sprinting, true, Enum.KeyCode.LeftShift)
ContextActionService:UnbindAction("DisableControls")
if StarterGui:FindFirstChild("TouchGui") then StarterGui.TouchGui.Enabled = true end
canCarry = true
GPAnimation:Stop(0)
HumanoidRootPart.Anchored = false
trail:Destroy()
end
end)
end)
elseif currState == Enum.HumanoidStateType.Running then
crouching = true
canCarry = true
Idle:Play(0.5, 2, 1 / (Humanoid.Health / 100))
onCrouchConnection = Humanoid.Running:Connect(function(speed)
if speed > 0.01 then
Walk:Play(0.4, 2, (Humanoid.WalkSpeed / 7) / 2)
Idle:Stop(0.4)
else
Walk:Stop(0.5)
Idle:Play(0.5, 2, 1 / (Humanoid.Health / 100))
end
end)
print("crouching")
Humanoid.JumpPower = JPTypes.crouch
Humanoid.WalkSpeed = math.round(WSTypes.crouch)
canDJ = false
ContextActionService:UnbindAction("Sprinting")
end
elseif crouching then
crouching = false
canCarry = true
Humanoid.JumpPower = JPTypes.norm
Humanoid.WalkSpeed = WSTypes.norm
onCrouchConnection:Disconnect()
for _, track:AnimationTrack in ipairs(Animator:GetPlayingAnimationTracks()) do
if track.Name:lower() == "crouchidle" or track.Name:lower() == "crouchwalk" then
track:Stop(0.7)
end
end
ContextActionService:BindAction("CrouchOrGP", CrouchOrGP, true, Enum.KeyCode.LeftControl)
ContextActionService:BindAction("Sprinting", Sprinting, true, Enum.KeyCode.LeftShift)
end
end
end
end
… this.
I’m working on a function that is compatible for keyboard and mobile. It does work, but it’s just messy; repeating the same type of function.
How can I have it so that I can merge the two codes, so I can work on one and it’ll be for both the keyboard and mobile (if that makes sense)?