How to improve ontouch script based on part count

I wonder if theres better way to improve this.

Script;


function onTouched(hit)
	local hit = hit.Parent:FindFirstChild("Humanoid")

	if hit  then
		print("function suppose")

		script.Parent.Transparency=1
		script.Parent.CanCollide = false
		wait(7)

		script.Parent.Transparency=0
		script.Parent.CanCollide = true
		wait(5)
	end


end



script.Parent.Touched:Connect(onTouched)

what your toughts about limited force on parts and semi anchored, about this issue.

place:Ontouch event script test - Roblox

Maybe this one could help -

local delay = 4

function onTouched(hit)
	local hit = hit.Parent:FindFirstChild("Humanoid")
	script.Parent.Transparency = if hit then 1 else 0
    script.Parent.CanCollide = hit == nil
	task.wait(delay)

end

script.Parent.Touched:Connect(onTouched)

[Make sure to add a debounce if you need]

3 Likes

That line doesnt work properly.

1 Like

Here’s a fix

script.Parent.Transparency = if hit then 1 else 0
script.Parent.CanCollide = hit == nil
2 Likes

Try the updated one, I had a mistake. [@JarodOfOrbiter fixed]

1 Like

adding to what @Valkyrop said

local delay = 4

function onTouched(hit)
	local hit = hit.Parent:FindFirstChild("Humanoid")
	script.Parent.Transparency = if hit then 1 else 0
    script.Parent.CanCollide = hit == nil
	task.wait(delay)

end

local PartFolder = --path to parts

for i,v in pairs(PartFolder:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(onTouched)
	end
end
3 Likes

got an error from that script. ; Workspace.PartFolder.Part.Script:13: Expected identifier when parsing expression, got ‘for’ -

1 Like

did you even remove the comment properly?