Hello, I would like to know how I can touch multiple parts efficiently without any bottlenecking.
The only parts I want to be able to be touched are in a folder in Workspace.
I also am trying to fire a remote event every time you press E near one of them, while touching them. Is there a better way to tackle what I’m trying to do?
Yes, I do have a script I can show you:
I want to get rid of the in pairs loop because it is inefficient.
local Player = game.Players.LocalPlayer
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
local TS = game:GetService("TweenService")
local Active = false
local UIS = game:GetService("UserInputService")
local Db = game:GetService("Debris")
for i,v in pairs(workspace.Wormholeable:GetChildren()) do
if v:IsA("Part") and v:FindFirstChild("TouchRange") then
local Clone = game.Workspace.WormholeButtonGui:Clone()
Clone.Parent = v
v.Touched:Connect(function()
Active = true
Clone.Enabled = true
TS:Create(Clone, TweenInfo.new(1, Enum.EasingStyle.Exponential), {ExtentsOffset = Vector3.new(0,1,0)}):Play()
end)
UIS.InputBegan:Connect(function(Input, GPE)
if GPE then
return
end
if Input.KeyCode == Enum.KeyCode.E and Active then
Active = true
TS:Create(v, TweenInfo.new(3, Enum.EasingStyle.Exponential), {Transparency = 1}):Play()
v.Anchored = false
v.CanCollide = false
Db:AddItem(v, 4)
game.ReplicatedStorage.Events.Wormhole:FireServer()
end
end)
v.TouchEnded:Connect(function()
if Active then
Active = false
TS:Create(Clone, TweenInfo.new(1, Enum.EasingStyle.Exponential), {ExtentsOffset = Vector3.new(0, 0, 0)}):Play()
end
end)
end
end
So as I understand it take all the Parts you want in the Explorer window to not be calculated when touched and make the CanTouch property false. They shouldn’t fire a Touched event after that.
if you don’t care which parts are touching, just that multiple are you can count how many .Touched events vs .TouchEnded you have received. Here I’ve converted your Active boolean to a number and tracked the ins and outs of touched events.
--!strict
local Player = game.Players.LocalPlayer
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
local TS: TweenService = game:GetService("TweenService")
local ActiveTouches: number = 0
local MustTouchAtLeast: number = 1
local UIS = game:GetService("UserInputService")
local Db = game:GetService("Debris")
for i,v in pairs(workspace.Wormholeable:GetChildren()) do
if v:IsA("Part") and v:FindFirstChild("TouchRange") then
local Clone = game.Workspace.WormholeButtonGui:Clone()
Clone.Parent = v
v.Touched:Connect(function()
ActiveTouches += 1
if ActiveTouches > 0 then -- only tween if touching at least one object
Clone.Enabled = true
TS:Create(Clone, TweenInfo.new(1, Enum.EasingStyle.Exponential), {ExtentsOffset = Vector3.new(0,1,0)}):Play()
end
end)
UIS.InputBegan:Connect(function(Input, GPE)
if GPE then
return
end
if Input.KeyCode == Enum.KeyCode.E and ActiveTouches >= MustTouchAtLeast then
TS:Create(v, TweenInfo.new(3, Enum.EasingStyle.Exponential), {Transparency = 1}):Play()
v.Anchored = false
v.CanCollide = false
Db:AddItem(v, 4)
game.ReplicatedStorage.Events.Wormhole:FireServer()
end
end)
v.TouchEnded:Connect(function()
ActiveTouches -= 1
if ActiveTouches <= 0 then -- un-tween if touching no objects
-- shouldn't get lower than 0 theoretically
TS:Create(Clone, TweenInfo.new(1, Enum.EasingStyle.Exponential), {ExtentsOffset = Vector3.new(0, 0, 0)}):Play()
end
end)
end
end