Hello , 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 .
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()
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.
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.
@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 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
@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