I’ve got two functions, one that stores offsets of parts in a model relative to a given anchorpoint, another that resets all parts to the offset they originally had. (So if the anchor would move, they’d be updated and it would look like it all moves at once
I have an empty table in which I cache all the offsets.
local partOffsetsMain = {}
local function RecalculateParts(Model,Anchorpoint,datastore)
for _, v in pairs (Model:GetChildren()) do
if v ~= Anchorpoint then
v.CFrame = Anchorpoint.CFrame * datastore[v]
end
end
end
local function StoreOffsets(Model,Anchorpoint,datastore)
for _, v in pairs (Model:GetChildren()) do
if v ~= Anchorpoint then
datastore[v] = Anchorpoint.CFrame:toObjectSpace(v.CFrame)
end
end
return true
end
I then call it as soon as an event is fired from the server to the client. I begin with clearing out the cache from previous data and then refill it with the new model.
Remotes["InteractDoor"].onClientEvent:connect(function(args)
-- args[2] refers to the model.
partOffsetsMain = {}
repeat wait() until StoreOffsets(args[2].Door,args[2].Door.Hinge,partOffsetsMain) == true
Run.RenderStepped:connect(function()
if RecalculationEnabled.Door == true then
RecalculateParts(args[2].Door,args[2].Door.Hinge,partOffsetsMain)
end
end)
if args[1] == "Open" then
local origin1 = args[2].Door.Hinge.CFrame
local OpenDoorAnimation = TweenService:Create(args[2].Door.Hinge,TweenInfos.Door.Open,{CFrame = origin1 * CFrame.Angles(0,math.rad(-108),0)})
RecalculationEnabled.Door = true
OpenDoorAnimation:Play()
OpenDoorAnimation.Completed:connect(function()
RecalculationEnabled.Door = false
end)
elseif args[1] == "Close" then
local origin1 = args[2].Door.Hinge.CFrame
local CloseDoorAnimation = TweenService:Create(args[2].Door.Hinge,TweenInfos.Door.Close,{CFrame = origin1 * CFrame.Angles(0,math.rad(108),0)})
RecalculationEnabled.Door = true
CloseDoorAnimation:Play()
CloseDoorAnimation.Completed:connect(function()
RecalculationEnabled.Door = false
end)
end
end)
The problem is that the first time I approach a door and press G, which triggers the event, it works fine. But when I approach a new door, and thus a new model is given to the functions, I get the error:
01:17:46.428 - Players.thelolguy301.PlayerScripts.Interaction:34: bad argument #2 to ‘?’ (Vector3 expected, got nil)
Thus stating that the data does not exist.
After doing some testing I found out that the data DOES in fact exist; I had my script return the amount of values in partOffsetsMain and it always resulted in 5, being the amount of parts in my door models.