Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Need help with this GetChildren Function?

I have a function trying to turn people invisible but it refuses to work or give me an error message, how would i fix it?


local limit = 5
local Site = script.Parent.Parent.Parent.Site
local TargetSite = script.Parent.Parent.Parent.Parent.Unit1.Site

function Demat(model) 
    print("demating")
    for i, v in pairs(model:GetChildren()) do
        if v:IsA("Part") then
            v.Transparency = v.Transparency + 1
        elseif v:IsA("Accessory") then
            v.Handle.Transparency = v.Handle.Transparency + 1
        elseif v:IsA("Accoutrement") then
            v.Handle.Transparency = v.Handle.Transparency + 1
        end
    end
end



function onClicked() 
if (script.Parent.Parent:FindFirstChild("Active")~= nil) then
local c = game.Players:GetChildren() 
for i = 1, #c do 
if (c[i].Character ~= nil) and ((c[i].Character.Torso.Position - script.Parent.Position).magnitude < limit) then 
Site.UpperFX.Enabled = true
Site.LowerFX.Enabled = true
Site.PointLight.Enabled = true
Site.BeamSound:Play()
c[i].Character.Torso.Anchored = true
Demat()
wait(4)
c[i].Character.Torso.Anchored = false
wait(0.01)
c[i].Character.Torso.CFrame = CFrame.new(script.Parent.Parent.Parent.Parent.Unit1.Site.Position + Vector3.new (math.random(-10,10)))
wait(0.01)
c[i].Character.Torso.Anchored = true
Site.UpperFX.Enabled = false
Site.LowerFX.Enabled = false
Site.PointLight.Enabled = false
TargetSite.UpperFX.Enabled = true
TargetSite.LowerFX.Enabled = true
TargetSite.PointLight.Enabled = true
TargetSite.BeamSound:Play()
wait(4)
TargetSite.UpperFX.Enabled = false
TargetSite.LowerFX.Enabled = false
TargetSite.PointLight.Enabled = false
c[i].Character.Torso.Anchored = false
end
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

1 answer

✓ Accepted answer

First, style.

  • You should mostlikely use :IsA("Part") instead of .ClassName == "Part".
  • tab your code correctly.
  • Use elseif for consecutive ifs where only one of them can happen. It's shorter and easier on the eyes
  • Put a space after , and don't put a space after pairs

Demat is close to being right, as far as I can tell. However, it's acting on the players service when it is clearly meant to act on a model.

It should take which model as a parameter:

function Demat(model) 
	print("demating")
	for i, v in pairs(model:GetChildren()) do
		if v:IsA("Part") then
			v.Transparency = v.Transparency + 1
		elseif v:IsA("Accessory") then
			v.Handle.Transparency = v.Handle.Transparency + 1
		elseif v:IsA("Accoutrement") then
			v.Handle.Transparency = v.Handle.Transparency + 1
		end
	end
end

When called, Demat should hide all of the parts in it.

The question for you, is which model?

If it's based on touching something, it's pretty easy to just call Demat from a Touched event handler:

script.Parent.Touched:connect(function(hit)
	-- get the model that contains the part
	local model = hit.Parent

	-- get the player from the character model
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		-- (it's a player and not some random object in the world)
		Demat(model)
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.