Need help with Collection Service

Hello :wave:, I am a modeller and I want to learn to script. Can you help me with Collection Services? This might just be me making stupid mistakes :face_with_head_bandage: .

I am new to scripting and I have been trying to learn how to use Collection Services to make lava as I would be using a lot of scripts if I make every individual script (Reduce Lag). Is there any way of tweaking/fixing the Collection system for lava? Here is the script:

local player = game.Workspace.LocalPlayer.Humanoid.Health

for i,v in pairs(CollectionService:GetTagged("Lava"))do
	for _, TaggedParts in pairs(TaggedParts) do
		while true do
			function OnTouch(Lava)
				local humanoid = Lava.Parent:FindFirstChild("Humanoid")
				if (humanoid ~= nil) then
					humanoid.Health = humanoid.Heatlth - 1
				wait(0.05)
			end
		end
	end
end
script.Parent.Touched:Connect(OnTouch()

image
image
image
What should I do to fix this? :thinking:

-Thanks :smiley:

Ok, Thanks
I will try that
:+1:

@0ssm4n It does not seem to work. Am I doing this correctly?

local CollectionService = game:GetService("CollectionService")
local player = game.Workspace.LocalPlayer.Humanoid.Health

for i,v in pairs(CollectionService:GetTagged("Lava"))do
	for _, TaggedParts in pairs(TaggedParts) do
		while true do
			script.Parent.Touched:Connect(OnTouch()
				local humanoid = Lava.Parent:FindFirstChild("Humanoid")
					if (humanoid ~= nil) then
					humanoid.Health = humanoid.Heatlth - 1
				wait(0.05)
				end
	end
end
script.Parent.Touched:Connect(OnTouch()

I see you put a local script in server script services. Yeah that doesn’t work local scripts only work in certain containers like player gui and such. Try copy and pasting the code in a server script or script.

So @dthecoolest Is this correct?
image

And also; I have noticed a red underline under the word local.


(Snip tool don’t snip the red line so I drew it in)

Here is the Output:

The error tells you that missed a parenthesis. This parenthesis belongs to the touched connection which should wrap the if statement.

Also don’t use a while true do loop I believe you are looking for a debounce to prevent the lava from damaging the player multiple times instantly as you can trigger the on touch event multiple times as both the left leg and the right leg can trigger the event multiple times.

@0ssm4n I have tried the script that you have provided. It seems to not work for me.

Output:

@dthecoolest This is what I got. I don’t understand the debounce thing but I know what it is for:

local CollectionService = game:GetService("CollectionService")
local player = game.Workspace.LocalPlayer.Humanoid.Health

for i,v in pairs(CollectionService:GetTagged("Lava"))do
	for _, TaggedParts in pairs(TaggedParts) do

		local humanoid = Lava.Parent:FindFirstChild("Humanoid")
		script.Parent.Touched:Connect(OnTouch())
		if (humanoid ~= nil) then
			humanoid.Health = humanoid.Heatlth - 1
			wait(0.05)
			print("Damage:Lava")
		else 
			ptint("Damage:nil")
		end
	end
end)

I don’t know if that would be correct or not?

I don’t really understand what you’re trying to do, why the script.Parent.Touched line? You wouldn’t need to do any script.Parent.Touched anymore cause you’re not doing any individual scripts inside each parts.

Besides, based on your logic, the script.Parent leads to the ServerScriptService.

local CollectionService = game:GetService("CollectionService")

local function kill(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end

for _,lavaBrick in pairs(CollectionService:GetTagged("Lava")) do
    lavaBrick.Touched:connect(kill)
end

This example should do the job for you.

@astra_wr The reason why I have used the touch thing is because the player would have to touch the Lava parts in order to take damage (slowly takes damage). And I am new to this so I don’t really know how to code in Roblox Lua so my logic is kinda bad. But anyways with your script is it possible to change it so that it would be able to slowly take damage?

Yes, if you’d want to make it so it only substracts a little, you can do this instead:

local CollectionService = game:GetService("CollectionService")

local function damage(part)
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid:TakeDamage(1)
    end
end

for _,lavaBrick in pairs(CollectionService:GetTagged("Lava")) do
    lavaBrick.Touched:connect(damage)
end
1 Like

Ok, I will try that. Hopefully, this works :crossed_fingers:

It seems like it is not working. Maybe it needs to find the location of the lava bricks. This is the error:
image

It seems like an issue on your side, where you’re tagging the lava bricks. Could you provide me with the code?

The one that uses :AddTag(). You also have to make sure that it is a brick (BasePart), not a model and so on.

Thanks @astra_wr , @dthecoolest and @0ssm4n for the help. It is working now.

  • Best of luck to you guys :smile:
1 Like