Skip to content

Instantly share code, notes, and snippets.

@dig1t
Last active April 23, 2025 03:13
Show Gist options
  • Save dig1t/d8b240ccd41a429d56c2aaa9c0512618 to your computer and use it in GitHub Desktop.
Save dig1t/d8b240ccd41a429d56c2aaa9c0512618 to your computer and use it in GitHub Desktop.
Creating a lava part using CollectionService
--!strict
local CollectionService = game:GetService("CollectionService")
-- This will be the tag you'll be adding
-- to every part that can damage players
local LAVA_TAG = "Lava"
-- We'll save our connections here so we can clean them up later
local connections: { [BasePart]: RBXScriptConnection } = {}
local function addPart(part: BasePart)
-- Make sure the instance is a part
if not part:IsA("BasePart") then
return
end
local connection = part.Touched:Connect(function(hit: BasePart)
if not hit.Parent then
return
end
local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
-- Make sure the humanoid exists and is alive
if humanoid and humanoid.Health > 0 then
humanoid:TakeDamage(humanoid.MaxHealth)
end
end)
-- We want to save our connection
connections[part] = connection
end
-- This will add a part to the connections table
-- when it is added to the workspace
CollectionService:GetInstanceAddedSignal(LAVA_TAG):Connect(addPart)
-- We want to set up any parts that are already in the world
for _, part in CollectionService:GetTagged(LAVA_TAG) do
addPart(part)
end
-- Clean up our connections when the script is destroyed
CollectionService:GetInstanceRemovedSignal(LAVA_TAG):Connect(function(part)
if connections[part] then
connections[part]:Disconnect()
connections[part] = nil
end
end)

To set up a lava part you need to create a part in the workspace and add the tag Lava to the new part.

This can be done in 2 ways:

  • Select your part, then in the properties panel, find the Tags section and add Lava as a tag.
  • With code:
local lavaPart = Instance.new("Part")
lavaPart.Parent = workspace

lavaPart:AddTag("Lava")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment