Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed CreatePart and Clone failing to work under Content Streaming #150

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions Core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local Cryo = require(Tool.Libraries:WaitForChild('Cryo'))
Support.ImportServices();
SyncAPI = Tool.SyncAPI;
Player = Players.LocalPlayer;
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService('RunService')

-- Preload assets
Expand Down Expand Up @@ -157,7 +158,6 @@ Enabled = Signal.new()
Disabled = Signal.new()

function Enable(Mouse)

-- Ensure tool is disabled or disabling, and not already enabling
if (IsEnabled and not IsDisabling) or IsEnabling then
return;
Expand Down Expand Up @@ -549,12 +549,34 @@ function CloneSelection()
-- Clones selected parts

-- Make sure that there are items in the selection
if #Selection.Items == 0 then
local AmountSelected = #Selection.Items

if AmountSelected == 0 then
return;
end;

-- Send the cloning request to the server
local Clones = SyncAPI:Invoke('Clone', Selection.Items, GetHighestParent(Selection.Items))
local CloneTag = SyncAPI:Invoke('Clone', Selection.Items, GetHighestParent(Selection.Items))

-- Attempt to collect all the clones

-- Strong chance that by now, most of the cloned objects would've of replicated.
local Clones = CollectionService:GetTagged(CloneTag)

-- However, with enough cloned parts, it's possible they would've of missed.
local Connection = CollectionService:GetInstanceAddedSignal(CloneTag):Connect(function(Object)
table.insert(Clones, Object)
end)

-- Wait for all the instances to be collected.
local TotalWaited = 0

-- At most, wait for half of a second for all of them. Otherwise, they likely out of range.
while #Clones ~= AmountSelected and TotalWaited <= 0.5 do
TotalWaited += task.wait()
end

Connection:Disconnect()

-- Put together the history record
local HistoryRecord = {
Expand Down
17 changes: 12 additions & 5 deletions SyncAPI.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local CollectionService = game:GetService("CollectionService")
local HttpService = game:GetService('HttpService')
local RunService = game:GetService('RunService')

Expand Down Expand Up @@ -57,19 +58,21 @@ Actions = {
end

local Clones = {}
local CloneTag = string.format("F3X:%s", HttpService:GenerateGUID())

-- Clone items
for _, Item in pairs(Items) do
local Clone = Item:Clone()
Clone.Parent = Parent

CollectionService:AddTag(Clone, CloneTag)

-- Register the clone
table.insert(Clones, Clone)
CreatedInstances[Item] = Item
end

-- Return the clones
return Clones
-- Return tag the clones will have
return CloneTag
end;

['CreatePart'] = function (PartType, Position, Parent)
Expand Down Expand Up @@ -99,8 +102,12 @@ Actions = {
-- Register the part
CreatedInstances[NewPart] = NewPart;

-- Create the tag to watch for.
local NewPartTag = string.format("F3X:%s", HttpService:GenerateGUID())
CollectionService:AddTag(NewPart, NewPartTag)

-- Return the part
return NewPart;
return NewPartTag;
end;

['CreateGroup'] = function (Type, Parent, Items)
Expand Down
28 changes: 23 additions & 5 deletions Tools/NewPart.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local UI = Tool:WaitForChild('UI')
local Libraries = Tool:WaitForChild('Libraries')

-- Services
local CollectionService = game:GetService("CollectionService")
local ContextActionService = game:GetService 'ContextActionService'

-- Libraries
Expand Down Expand Up @@ -179,13 +180,30 @@ end;
function CreatePart(Type)

-- Send the creation request to the server
local Part = Core.SyncAPI:Invoke('CreatePart', Type, CFrame.new(Core.Mouse.Hit.p), Core.Targeting.Scope)
local PartTag = Core.SyncAPI:Invoke('CreatePart', Type, CFrame.new(Core.Mouse.Hit.p), Core.Targeting.Scope)

-- Make sure the part creation succeeds
if not Part then
return;
end;
-- Try getting the part the first time.
local Part = CollectionService:GetTagged(PartTag)[1]

if not Part then
-- Let's try waiting for the part to stream in.
local Connection = CollectionService:GetInstanceAddedSignal(PartTag):Once(function(Object)
Part = Object
end)

-- Try waiting for the part's existence.
local TotalWaited = 0
while not Part and TotalWaited <= 0.5 do
TotalWaited += task.wait()
end

-- If there still wasn't a part, then it's probably out of range.
if not Part then
Connection:Disconnect()
return
end
end

-- Put together the history record
local HistoryRecord = {
Part = Part;
Expand Down