i am making a obby game so there is a lots of brick thats need to script .if you just put the script in all the brick that will be lag. so i want to make a handler for it . for the example like disappearing brick i want to put all the disappearing brick in a folder , and make a handler for all the disappearing brick. (i want the brick to if a player touch the brick it will disappear) so how do i script the handler
Personally, I would iterate through all the bricks in the folder and give each of them a function that fires on .Touched. Iâll check if it was a player that touched it and then destroy the brick. If you want the brick to completely vanish I would use :Destroy, but if you want it to stay, just change itâs transparency to 1 and switch CanCollide to false (or just reparent the brick to somewhere like serverstorage and reparent it to workspace)
Alright, there is some misconceptions here on what will cause lag. But what your asking for is easy.
Make a folder and place them all in it.
-
In script get all decedents of that folder
-
Do a for loop and do an IsA(âPartâ)
Check
When it finds a valid grab that part and you can connect a function to the touched event to kill the brick
You have some options here, you can move the brick to make it disappear, you can destroy it. Or set transparency to 1 and set it to not collide. So that will be up to you.
When you say handler, you may be referring to an event handler and in that case when you do the for loop you need to fire an event in response that passes the part ref with it and you can process outcome elsewhere if you ao please
CS is a good service indeed. I would agree that is another option, but takes a touch to get used too.
Since you need a 3rd party plugin to pretag, otherwise you have script go through all the parts and tag them at runtime
Yeah, but really it doesnât require a lot of learning, you just need to know two essential methods, :AddTag()
and :GetTagged()
, and using the plugin doesnât even need instructions really
You can achieve this by putting the script in serverscriptservice and doing one of the following:
- Iterate through the folderâs children containing the parts
for i, v in pairs(Folder_With_Parts:GetChildren()) do
--Code here
end
- You can also use âCollection Serviceâ, You add tags to the items you want affected by using :AddTag() or by using a plugin such as Sweetheartichokeâs Tag Editor and add the Tags from there. You then proceed to iterate through the items using the :GetTagged() Syntax as shown:
local parts = game:GetService("CollectionService"):GetTagged("DissapearingBricks")
for i, v in pairs(parts) do
--Code here
end
In addition, Donât forget to add a Debounce when the touch event is fired as to not cause any lag:
local parts = game:GetService("CollectionService"):GetTagged("DissapearingBricks")
local db = false
for i, v in pairs(parts) do
v.Touched:Connect(function()
if db == false then
db = true
--code here
end
end)
end
Good to mention the âdebounceâ, but your code example is slightly wrong, as it:
- does not set the
db
variable back tofalse
(or maybe it is intentional?), - only has one debounce variable that is used for all bricksâ touch-event.
(BTW: Please use indentation when writing code, as it helps to understand the structure of the code.)
To give each individual brick a âdebounceâ-indicator, then perhaps a dictionary could be used, where the key is the brick-object itself:
local parts = game:GetService("CollectionService"):GetTagged("DisappearingBricks")
local bricksDebounce = {}
for i, v in ipairs(parts) do
v.Touched:Connect(function()
if not bricksDebounce[v] then
bricksDebounce[v] = true
-- code here
bricksDebounce[v] = nil
end
end)
end
Though depending on what XxAwesomeDabxX means with âdisappearingâ, if it is supposed to be instantaneous or perhaps just fade away, then the touch-event (and debounce) could be changed, into something like:
local rbxConnections = {}
local function RemoveConnection(part)
if rbxConnections[part] then
rbxConnections[part]:Disconnect()
rbxConnections[part] = nil
end
end
local TweenService = game:GetService("TweenService")
local function MakePartDisappear(part)
-- Fade away within 1 second, then destroy the part
local tweenInfo = TweenInfo.new(1.0)
local tween = TweenService:Create(part, tweenInfo, { Transparency = 1 })
tween.Completed:Connect(function()
part:Destroy()
end)
tween:Play()
end
local parts = game:GetService("CollectionService"):GetTagged("DisappearingBricks")
for _,v in ipairs(parts) do
if v:IsA("BasePart") then
rbxConnections[v] = v.Touched:Connect(function(hit)
RemoveConnection(v)
MakePartDisappear(v)
end)
end
end
is there any easier way cuz this is too advance i dont understand
Hereâs an easy one.
while true do
wait(0.1)
for i,v in pairs (folder_with_the_parts_in_it) do
v.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
wait(2)
v.Transparency = 1
v.CanCollide = false
wait(2)
v.Transparency = 0
v.CanCollide = true
end
end)
end
end
Although, itâs not the best performance.
why its said [ServerScriptService.Script:5: bad argument #1 to âpairsâ (table expected, got Object)]
oh whoops, heres fixed code
while true do
wait(0.1)
for i,v in pairs (folder_with_the_parts_in_it:GetChildren()) do
v.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
wait(2)
v.Transparency = 1
v.CanCollide = false
wait(2)
v.Transparency = 0
v.CanCollide = true
end
end)
end
end
You have to put :GetChildren() at the end
umm why have to wait 0.1 at first
so your game doesnât crash lol
Take your time to read the code. Even though it might âseem advancedâ, if you âslow downâ and read it, line by line, statement by statement, it is no different from what others suggest of code.
And do use the Roblox Engine API Reference | Documentation - Roblox Creator Hub to search for the things you do not yet know how works.
Yeah, roblox teaches you how to code.
Or search on youtube, theres many ways.
yeah but its really hard to understand
i learn basic scripting from copying games
lol im stupid so i use this
local folder = workspace:FindFirstChild(âFolderâ)
while true do
wait()
for i,v in pairs (folder:GetChildren()) do
v.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
v.Transparency = 0.1
wait(0.1)
v.Transparency = 0.2
wait(0.1)
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.4
wait(0.1)
v.Transparency = 0.5
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.7
wait(0.1)
v.Transparency = 0.8
wait(0.1)
v.Transparency = 0.9
wait(0.1)
v.Transparency = 1
v.CanCollide = false
wait(2)
v.CanCollide = true
v.Transparency = 0.9
wait(0.1)
v.Transparency = 0.8
wait(0.1)
v.Transparency = 0.7
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.5
wait(0.1)
v.Transparency = 0.4
wait(0.1)
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.2
wait(0.1)
v.Transparency = 0.1
wait(0.1)
v.Transparency = 0
end
end)
end
end