Currently I am working on the first class for my class fighting game but have run into a problem I am clueless how to solve. When the player touches a part, they are teleported and equipped with the class’ outfit and weapon. The teleportation and outfit work fine, but the weapon goes all over the place.
Here is a video showing the issue:
In the video, I touch the part which turns you into the class. This successfully teleports the player to another area and welds the outfit to their character, as I mentioned previously. However, the weapon does not work properly. When idle, the weapon moves too far, and in the other animations, it flies around.
Here is the code for the part:
local part = script.Parent
local SS = game.ServerStorage
local arsenals = SS.ClassArsenals
local storedWarriorArsenal = arsenals.WarriorArsenal
local used = false
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not used then
used = true
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local HRP = character.HumanoidRootPart
local torso = character.Torso
local rightArm = character.RightArm
local leftArm = character.LeftArm
local rightLeg = character.RightLeg
local leftLeg = character.LeftLeg
local animate = character.Animate
--Arsenal Components
local warriorArsenal = storedWarriorArsenal:Clone()
local torsoPiece = warriorArsenal.TorsoPiece.Plastic
local rightArmPiece = warriorArsenal.RightArmPiece.Plastic
local leftArmPiece = warriorArsenal.LeftArmPiece.Plastic
local rightLegPiece = warriorArsenal.RightLegPiece.Plastic
local leftLegPiece = warriorArsenal.LeftLegPiece.Plastic
local weapon = warriorArsenal.Weapon.Plastic
local torsoWeld = Instance.new("Weld")
local rightArmWeld = Instance.new("Weld")
local leftArmWeld = Instance.new("Weld")
local rightLegWeld = Instance.new("Weld")
local leftLegWeld = Instance.new("Weld")
local weaponJoint = Instance.new("Motor6D")
--Animate Scripts
local currentAnimate = character.Animate
local warriorAnimate = SS.AnimateScripts.WarriorAnimate:Clone()
HRP.Position = Vector3.new(0, 50, 0)
--Weld armor to the player's character and create the weapon's joint so it can be animated.
torsoPiece.CFrame = torso.CFrame
torsoWeld.Part0 = torso
torsoWeld.Part1 = torsoPiece
torsoWeld.C0 = torso.CFrame:Inverse()
torsoWeld.C1 = torsoPiece.CFrame:Inverse()
torsoWeld.Parent = torso
rightArmPiece.CFrame = rightArm.CFrame
rightArmWeld.Part0 = rightArm
rightArmWeld.Part1 = rightArmPiece
rightArmWeld.C0 = rightArm.CFrame:Inverse()
rightArmWeld.C1 = rightArmPiece.CFrame:Inverse()
rightArmWeld.Parent = rightArm
leftArmPiece.CFrame = leftArm.CFrame * CFrame.Angles(0, math.rad(180), 0)
leftArmWeld.Part0 = leftArm
leftArmWeld.Part1 = leftArmPiece
leftArmWeld.C0 = leftArm.CFrame:Inverse()
leftArmWeld.C1 = leftArmPiece.CFrame:Inverse()
leftArmWeld.Parent = leftArm
rightLegPiece.CFrame = rightLeg.CFrame
rightLegWeld.Part0 = rightLeg
rightLegWeld.Part1 = rightLegPiece
rightLegWeld.C0 = rightLeg.CFrame:Inverse()
rightLegWeld.C1 = rightLegPiece.CFrame:Inverse()
rightLegWeld.Parent = rightLeg
leftLegPiece.CFrame = leftLeg.CFrame * CFrame.Angles(0, math.rad(180), 0)
leftLegWeld.Part0 = leftLeg
leftLegWeld.Part1 = leftLegPiece
leftLegWeld.C0 = leftLeg.CFrame:Inverse()
leftLegWeld.C1 = leftLegPiece.CFrame:Inverse()
leftLegWeld.Parent = leftLeg
weapon.CFrame = rightArm.CFrame * CFrame.new(0, -1.1, -1.35) --Offsets the weapon to be held in the correct position and orientation.
weaponJoint.Part0 = rightArm
weaponJoint.Part1 = weapon
weaponJoint.C0 = rightArm.CFrame:Inverse()
weaponJoint.C1 = weapon.CFrame:Inverse()
weaponJoint.Parent = rightArm
warriorArsenal.Parent = character
--Switch out the current animate script with this class' animate script.
currentAnimate:Destroy()
warriorAnimate.Name = "Animate"
warriorAnimate.Parent = character
wait(0.1)
used = false
end
end)
The outfit is welded to the character no problem, but with the weapon I create a joint so it can be animated. In lines 92-94, I switch out the current animate script with the class’ designated animate script. The old animations are very similar to the class’ animations, the only difference is that the weapon is supposed to be animated in the class’ animations. However, as you saw in the video, every part of the animations works properly except the weapon.
I have double checked that the animation ids are correct, so this is not the problem. The joint’s Part0 and Part1 are also correct. Something to note is that the weapon gets attached to the player’s character correctly when the animate scripts are not switched.
Here is a video showcasing what I mean:
As you can see, the weapon is attached properly to the character. However, the animations playing are not the class’ designated animations and are instead the old animations which do not animate the weapon, but I need the class’ animations to be playing so the weapon is animated, which I why I programmed lines 92-94 in the script above.
Here is also the code for the class’ designated animate script if it helps:
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local idleAnim = humanoid:LoadAnimation(script.IdleAnim)
local runAnim = humanoid:LoadAnimation(script.RunAnim)
local jumpAnim = humanoid:LoadAnimation(script.JumpAnim)
local fallAnim = humanoid:LoadAnimation(script.FallAnim)
local animations = {
idleAnim,
runAnim,
jumpAnim,
fallAnim,
}
idleAnim.Priority = "Core"
runAnim.Priority = "Core"
jumpAnim.Priority = "Movement"
fallAnim.Priority = "Movement"
local state --Used to ensure the proper animation is being played at the proper time.
--Idle and Run Animations Function
humanoid.Running:Connect(function(speed)
--Idle
if speed == 0 and not idleAnim.IsPlaying then
state = "idle"
if state == "idle" then
--Stops all playing animations.
for i, v in pairs(animations) do
v:Stop()
end
idleAnim:Play()
end
--Run
elseif speed ~= 0 and not runAnim.IsPlaying then
state = "running"
if state == "running" then
for i, v in pairs(animations) do
v:Stop()
end
runAnim:Play()
end
end
end)
--Jump and Fall Animations Function
humanoid.StateChanged:Connect(function(old, new)
--Jump
if new == Enum.HumanoidStateType.Jumping then
state = "jumping"
if state == "jumping" then
for i, v in pairs(animations) do
v:Stop()
end
jumpAnim:Play()
end
--Fall
elseif new == Enum.HumanoidStateType.Freefall then
state = "falling"
if old == Enum.HumanoidStateType.Jumping then
wait(0.2) --Approximate time it takes the player to reach their peak jump height.
if state == "falling" then
for i, v in pairs(animations) do
v:Stop()
end
fallAnim:Play()
end
else
if state == "falling" then
for i, v in pairs(animations) do
v:Stop()
end
fallAnim:Play()
end
end
end
end)
I am stuck on how to solve this, so if you have any ideas, please let me know!