I have no idea what I could be doing wrong, but for some reason I can’t seem to disconnect the function from the :MoveToFinished
event, so whenever I use Humanoid:MoveTo
, it fires even when it shouldn’t.
I’m working on a changing rooms system; when the player enters a store, the script starts checking for the distance between the player and the room, using RunService. Once the player is close enough and if the room is available, the player is automatically pulled inside the room using the :MoveTo function. Once the player is inside the room, the script opens the UI to customize the character, changes the camera position for an upclose view, etc.
The issue is that when I press the exist button to leave the changing room, I use :MoveTo
to move the character to the previous position outside the room, but for some reason it also fires the MoveToFinished function, even if I disconnected it right after the player initially walked into the changing room.
--SERVICES--
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local RunS = game:GetService("RunService")
local ZonePlus = require(RS:WaitForChild("Zone"))
--PLAYER INSTANCES--
local Plr = Players.LocalPlayer
local Character = Plr.Character or Plr.CharacterAdded:Wait()
local Controls = require(Plr.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
local Dummy
--REMOTES--
local RE = RS:WaitForChild("RoomsRemote")
local ShopData = RE:WaitForChild("RoomsInfo"):InvokeServer("GetData","Shops")
local RoomsData = RE.RoomsInfo:InvokeServer("GetData","ChangingRooms")
--OVERLAP PARAMS--
local OP = OverlapParams.new()
OP.FilterType = Enum.RaycastFilterType.Whitelist
OP.FilterDescendantsInstances = {Character}
OP.CollisionGroup = "Default"
--VARIABLES--
local DistanceChecks = {}
local inArea = false
local AreaName
local MT
local usingRoom = false
----FUNCTIONS----
local function startTryon(Background)
--Removed this part of the code 'cause it's not relevant to the problem.
--This part simply opens the UI and changes the camera position, nothing else.
end
local function enterRoom(Room,Background)
local Curtain = Room:FindFirstChild("Curtain")
Curtain.Transparency = 1
Curtain.CanCollide = false
Character:SetAttribute("ogPos",Character.HumanoidRootPart.Position)
Controls:Disable()
Character.Humanoid:MoveTo(Room.Platform.Position)
MT = Character.Humanoid.MoveToFinished:Connect(function(reached)
if not reached then
local charSize = Character:GetExtentsSize()
local cf = CFrame.new(0,charSize.Y/2,0)
local dummyCF = Room.Platform.CFrame:ToWorldSpace(cf)
Character.PrimaryPart.CFrame = dummyCF
end
task.wait(0.5)
if not usingRoom then
startTryon(Background)
Curtain.Transparency = 0.5
if MT ~= nil then
MT:Disconnect()
MT = nil
end --Tried to disconnect, but it doesn't seem to work.
--I also tried without this if statement,
--but for some reason it errors because MT is nil??
usingRoom = true
end
end)
end
local function checkDistanceRooms(room)
local Model = room.Model
DistanceChecks["Room"..Model:GetAttribute("ID")] = RunS.Stepped:Connect(function(time, delta)
if Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Dead and Plr:DistanceFromCharacter(Model.Platform.Position) <= 20 then
--I think this part runs more than once even if i put a conditional debounce with usingRoom??
if not Model:GetAttribute("isBusy") and not usingRoom then
task.wait()
local isBusy = RE.RoomsInfo:InvokeServer("isBusy",room.ID)
if isBusy then return
else
Model:SetAttribute("isBusy",true)
RE:FireServer(Model)
enterRoom(Model,room.Background)
print("near changing room ID: "..Model:GetAttribute("ID"))
end
end
end
end)
end
local function zoneCheck(Shop)
local Area = Shop.Area
local Bound = ZonePlus.new(Area)
Bound.localPlayerEntered:Connect(function()
inArea = true
AreaName = Area.Name
warn("player joined: "..Area.Name)
for i,room in pairs(RoomsData) do
task.spawn(function()
checkDistanceRooms(room)
end)
end
end)
Bound.localPlayerExited:Connect(function()
print("player left: "..Shop.ShopName)
for i,room in pairs(Shop.ChangingRooms) do
local ID = room:GetAttribute("ID")
DistanceChecks["Room"..ID]:Disconnect()
DistanceChecks["Room"..ID] = nil
end
end)
end
--This is where the problem is I believe, as you can see this function used :MoveTo.
--The problem is, I don't want the :MoveToFinished part above to fire, hence why I tried to use :Disconnect.
--Because it doesn't work, I tried to override it with another (empty) :MoveToFinished, but instead both functions are ran.
Plr.PlayerGui.ChangingRoom.Base.Leave.TextButton.MouseButton1Down:Connect(function()
usingRoom = false
Plr.PlayerGui.ChangingRoom.Enabled = false
workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
Character.Humanoid:MoveTo(Character:GetAttribute("ogPos") + Vector3.new(5,0,5))
Dummy:Destroy()
MT = Character.Humanoid.MoveToFinished:Connect(function(reached)
print("pls do nothing")
end)
print("PLS WORK omg")
Controls:Enable()
end)
for i,v in pairs(ShopData) do
zoneCheck(v)
end
I commented the code with extra informations, so please check it before replying.
Thanks in advance for helping!