Hello, I was wondering if there was a better way to going about what the script below does. I am more concerned about the script causing lag in this matter rather than much of anything else.
What the script does is simply get the children of a model and detect when a player touches any of the children. If a player touches the child, the childs transparency goes to 0. all of the other touching parts in the model that are touching the current child also get a transparency of 0. After that, a loop makes the transparency slowly get back to 1 for each part.
wait(math.random(1,2))
local model = {
game.Workspace.Part1;
game.Workspace.Part2;
game.Workspace.Part3;
game.Workspace.Part4;
game.Workspace.Part5;
game.Workspace.Part6;
}
----- ----------- ---- --------------
--Start:
for nam, obj in pairs(model) do
if obj.ClassName == "Part" then
--Get the object ready:----------
if obj.Tranparency ~= 1 then
obj.Transparency = 1
end
if obj:FindFirstChild("SoundFX") == nil then
local newsom = Instance.new("Sound")
newsom.SoundId = "rbxassetid://1324318825"
newsom.Name = "SoundFX"
newsom.Looped = false
newsom.Parent = game.ReplicatedStorage
newsom.Parent = obj
newsom:Stop()
end
if obj:FindFirstChild("Activator") == nil then
local newActivator = Instance.new("BoolValue")
newActivator.Name = "Activator"
newActivator.Parent = obj
end
------:Start:---------
local function GetTouchingParts(part)
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts()
for _, p in next, results do
if p.ClassName == "Part" and p:FindFirstChild("Activator") then
if p.Activator.Value == false then
wait(0.05)
p.Activator.Value = true
end
end
end;
connection:Disconnect()
return results
end
local activator = obj:FindFirstChild("Activator")
local actviator_ACTIVE = false
activator.Changed:Connect(function()
if activator.Value == true then
if actviator_ACTIVE == false then
actviator_ACTIVE = true
obj.Transparency = 0
local results = GetTouchingParts(obj)
while obj.Transparency < 1 do
wait(0.02)
obj.Transparency = obj.Transparency + 0.1
if obj.Transparency >= 1 then
obj.Transparency = 1
break;
end
end
wait(0.2)
activator.Value = false
actviator_ACTIVE = false
end
end
end)
local Plr_Touched = false
obj.Touched:Connect(function(hit)
if hit and hit.Parent then
if hit.Parent:FindFirstChild("Humanoid") then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if Plr_Touched == false then
Plr_Touched = true
if activator.Value == false then
activator.Value = true
end
wait(0.5)
Plr_Touched = false
end
end
end
end)
end
end