I am trying to create a mailman job, where the player’s character walks to a point then plays an animation but, if the character walks somewhere else while the character is walking to the point on the previous mailbox then goes back to the point again, the next mailbox the character triggers teleports to the previous mailbox. How can I fix this?
local cooldown = false
local cooldown2 = false
local connection
local connection2
local tweenService = game:GetService("TweenService")
local tweenInformation = TweenInfo.new(0.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.InOut,0,false,0)
local DataManager = require(game.ReplicatedStorage.DataManager)
local walkpart = script.Parent.Parent.Part
script.Parent.Triggered:Connect(function(plr)
local player = plr
if not cooldown and player.PlayerGui.Task.Tasks.Value == "Mail" and player.PlayerGui.Task.Tasks.Amount.Value == 1 then
local Character = plr.Character
Character.Humanoid:MoveTo(walkpart.Position)
connection = Character.Humanoid.MoveToFinished:Connect(function(reached)
if not cooldown2 then
local Anim = Instance.new("Animation")
Anim.AnimationId = 'rbxassetid://7794700511'
local PlayAnim = Character.Humanoid:LoadAnimation(Anim)
cooldown2 = true
cooldown = true
if Character.Humanoid.WalkToPoint == walkpart.Position then
Character.HumanoidRootPart.Anchored = true
Character.HumanoidRootPart.CFrame = walkpart.CFrame * CFrame.new(0,4.85,0)
local tween1 = tweenService:Create(Character.HumanoidRootPart,tweenInformation,{CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(0,Vector3.new(CFrame.new(Character.HumanoidRootPart.Position, script.Parent.Parent.Position):ToOrientation()).Y,0)})
tween1:Play()
wait(.1)
PlayAnim:Play()
end
connection2 = PlayAnim:GetMarkerReachedSignal("deliver"):Connect(function (paramString)
cooldown2 = true
wait(.5)
if connection then
connection:Disconnect()
connection = nil
end
if connection2 then
connection2:Disconnect()
connection2 = nil
end
if player.PlayerGui.Task.Tasks.Amount.Value == 1 and Character.Humanoid.WalkToPoint == walkpart.Position then
local gui = player.PlayerGui.Task:FindFirstChild("Mail")
Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
Character.Humanoid.WalkToPart = nil
Character.HumanoidRootPart.Anchored = false
if gui then
local data = DataManager:Get(player)
if player.PlayerGui.Task.Tasks.Amount.Value == 1 then
if data then
if player then
game.ReplicatedStorage.sound:FireClient(player)
data.cash+=50
data.exp += 10
end
end
local rand = math.random(1,5)
if rand == 1 then
player.PlayerGui.Task.Tasks.Amount.Value = 2
gui.Adornee = workspace.Mail.mailpart2
game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
end
if rand == 2 then
player.PlayerGui.Task.Tasks.Amount.Value = 3
gui.Adornee = workspace.Mail.mailpart3
game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
end
if rand == 3 then
player.PlayerGui.Task.Tasks.Amount.Value = 4
gui.Adornee = workspace.Mail.mailpart4
game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
end
if rand == 4 then
player.PlayerGui.Task.Tasks.Amount.Value = 5
gui.Adornee = workspace.Mail.mailpart5
game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
end
if rand == 5 then
player.PlayerGui.Task.Tasks.Amount.Value = 6
gui.Adornee = workspace.Mail.mailpart6
game.ReplicatedStorage.Jobs.Mail:FireClient(player,player.PlayerGui.Task.Tasks.Amount.Value)
end
end
end
end
wait(5)
cooldown2 = false
cooldown = false
end)
end
wait(8)
if connection then
Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
connection:Disconnect()
connection = nil
end
end)
end
end)