Animation unexpectedly flying apart from the start position
Im making a carry system that plays 2 animations on both the victim and the carrier (carry and idle animation). The script works for first time, but it gives unexpected result after that.
Server script:
local carryEntries = {}
local function weldCarry(carrier : Model, victim : Model)
local weld = Instance.new("WeldConstraint")
weld.Name = "_Carry"
weld.Parent = carrier
weld.Part0 = carrier.PrimaryPart
weld.Part1 = victim.PrimaryPart
victim.Humanoid.AutoRotate = false
victim.Humanoid.PlatformStand = true
for i, v : BasePart in victim:GetDescendants() do
if v:IsA("BasePart") then
v.Massless = true
v.CanCollide = false
end
end
end
local function unweldCarry(char)
if char:FindFirstChild("_Carry") then
-- on carrier character
char._Carry:Destroy()
else
-- on victim character
char.Humanoid.AutoRotate = true
char.Humanoid.PlatformStand = false
for i, v in char:GetDescendants() do
if v:IsA("BasePart") then
v.Massless = false
v.CanCollide = true
end
end
end
end
animHandler.OnServerInvoke = function(plr, victim)
local cached = carryEntries[plr.UserId]
if cached then
carryEntries[plr.UserId] = nil
plr.Character.Humanoid.WalkSpeed = 16
cached[1].Humanoid.WalkSpeed = 16
cached[2]:Stop()
cached[3]:Stop()
unweldCarry(plr.Character); unweldCarry(cached[1]);
return true
end
if not victim then return false end
local mainChar = plr.Character
mainChar.Humanoid.WalkSpeed = 0
victim.Humanoid.WalkSpeed = 0
local APickerCarry : AnimationTrack = mainChar.Humanoid:LoadAnimation(animationFolder.Picker.Carry)
local AVictimCarry : AnimationTrack = victim.Humanoid:LoadAnimation(animationFolder.Victim.Carry)
local APickerIdle : AnimationTrack = mainChar.Humanoid:LoadAnimation(animationFolder.Picker.Idle)
local AVictimIdle : AnimationTrack = victim.Humanoid:LoadAnimation(animationFolder.Victim.Idle)
carryEntries[plr.UserId] = {victim, APickerIdle, AVictimIdle,}
-- face the character
-- mainChar (carrier character model), victim (victim character model)
victim:PivotTo(CFrame.new(mainChar.PrimaryPart.Position + (mainChar.PrimaryPart.CFrame.LookVector * 6), mainChar.PrimaryPart.Position))
mainChar:PivotTo(CFrame.new(mainChar.PrimaryPart.Position, victim.PrimaryPart.Position))
-- play both of the animation
-- fixme: (victim) character unexpectedly moves away from the
-- carrier when playing animations below
APickerCarry:Play() -- "pick the victim"
AVictimCarry:Play() -- "react to the victim and get carried"
print((mainChar.PrimaryPart.Position-victim.PrimaryPart.Position).Magnitude) -- 6 studs
APickerCarry.Stopped:Wait()
print((mainChar.PrimaryPart.Position-victim.PrimaryPart.Position).Magnitude) -- 47 studs
weldCarry(mainChar, victim)
end
Client LocalScript
local input = game:GetService("UserInputService")
local remoteEvent = game.ReplicatedStorage.anim
local player = game.Players.LocalPlayer
local debounce = false
local isPicking = false
local distance_max = 7 -- in studs
input.InputEnded:Connect(function(key, gs)
if debounce then return end
if gs or key.KeyCode ~= Enum.KeyCode.C then return end
debounce = true
print("key", gs, key.KeyCode)
if isPicking then
-- uncarry (no arg needed)
local success, ret = pcall(remoteEvent.InvokeServer, remoteEvent)
isPicking = if success and ret then false else true
print("uncarried")
task.wait(.5)
debounce = false
return
end
for i, v in workspace:GetChildren() do
if not v:FindFirstChild("Humanoid") then continue end
if player.Character and player.Character ~= v and (player.Character.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude <= distance_max then
print("found", v)
local success, ret = pcall(remoteEvent.InvokeServer, remoteEvent, v)
isPicking = if success then ret else false
break
end
end
task.wait(.5)
debounce = false
end)
no issues with localscript currently
Expected:
This only work when playing the animation on first time
Bug:
The bug is reproducible by playing the animation again
what causes this bug??