So, I’ve been trying to make a module recently but I’ve got unexpected results. I want to turn a part/union/model into a black part with a transparency of 0.5 but instead, it only sets the player’s transparency/colour and not the other models or parts. I honestly don’t know why it’s only picking the player. A few moments later, I stopped it from picking the player but now it picked nothing else. Here’s the code(not the full code):
cast1.Touched:Connect(function(hit)
hasbeentouched = true
local clone = hit
local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if hasbeentouched == true then
if hum then
print("else")
else
if clone:IsA("Model") then
for i,v in pairs(clone:GetChildren()) do
v.BrickColor = BrickColor.new("Black")
v.Transparency = 0.5
v:SetPrimaryPartCFrame(hit.Position)
end
elseif clone:IsA("UnionOperation") or clone:IsA("Part") then
clone.Transparency = 0.5
clone.BrickColor = BrickColor.Black()
clone.Position = hit.Position
end
end
end
end)
end
Also, this is a module script and yes, don’t question if I called the function or the module script since I have. I have no idea on why this is happening.
local clone = hit
Here, you set clone to hit. When “hit” touches something, it doesn’t get the model. You need to get the model by doing “hit.Parent” (the character).
What you’ve done here is:
for i,v in pairs(clone:GetChildren()) do
v.BrickColor = BrickColor.new("Black")
v.Transparency = 0.5
v:SetPrimaryPartCFrame(hit.Position)
end
You’ve only looped through the children of “clone” (which is hit). So if the arm hit the object, you only looped through the children of the arm. What I think you meant to do is.
if clone.Parent:IsA("Model") then
-- and
for i,v in pairs(clone.Parent:GetChildren()) do
etc.
Unfortunately, the code now is silent(which is meaning that it’s doing nothing). I’ll show you my code:
cast1.Touched:Connect(function(hit)
hasbeentouched = true
local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if hasbeentouched == true then
if hum then
print("else")
else
if hit.Parent:IsA("Model") then
for i,v in pairs(hit.Parent:GetChildren()) do
v.BrickColor = BrickColor.new("Black")
v.Transparency = 0.5
v:SetPrimaryPartCFrame(hit.Position)
end
elseif hit:IsA("UnionOperation") or hit:IsA("Part") then
hit.Transparency = 0.5
hit.BrickColor = BrickColor.Black()
hit.Position = hit.Position
end
end
end
end)
end
Well, you’re doing a condition check. You’re saying if the humanoid exists, then do nothing (besides print “else”.
Is your goal to make the player’s body parts go black & transparent?
Yes, that’s indeed my goal. That’s my goal because I’m wanting to mimic shadows and how they look like and just incase you interpreted the problem incorrectly, it turned the player black and transparent instead of the models that I have.
This should be your code:
cast1.Touched:Connect(function(hit)
hasbeentouched = true
local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if hasbeentouched == true then
if hum then
-- (HUMANOID EXISTS, SO MAKE BODY PARTS TRANSPARENT)
if hit.Parent:IsA("Model") then
for i,v in pairs(hit.Parent:GetChildren()) do
v.BrickColor = BrickColor.new("Black")
v.Transparency = 0.5
v:SetPrimaryPartCFrame(hit.Position)
end
elseif hit:IsA("UnionOperation") or hit:IsA("Part") then
hit.Transparency = 0.5
hit.BrickColor = BrickColor.Black()
hit.Position = hit.Position
end
else
-- HUMANOID DOES NOT EXIST, MEANING ANOTHER PART/OBJECT TOUCHED
print("else")
end
end
end)
end
Remember.
if value then
This can account for if value == true or if value exists.
if not value then
This can account for if value == false, if value == nil or if the value does not exist.
(Nil/non existent are the same thing). These two things are the same
local value -- (nothing was set, so it's "nil")
local value = nil -- (value is specifically set to nil, so it's "nil")
Well, I reversed the code since I wanted it to do the opposite and well, it didn’t work. Code:
cast1.Touched:Connect(function(hit)
hasbeentouched = true
local hum = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if hasbeentouched == true then
if hum then
print("else")
else
if hit.Parent:IsA("Model") then
for i,v in pairs(hit.Parent:GetChildren()) do
v.BrickColor = BrickColor.new("Black")
v.Transparency = 0.5
v.Parent:SetPrimaryPartCFrame(hit.Position)
end
elseif hit:IsA("UnionOperation") or hit:IsA("Part") then
hit.Transparency = 0.5
hit.BrickColor = BrickColor.Black()
hit.Position = hit.Position
end
end
end
end)
end
Why did you the reverse the code? If your goal is to make the player’s body parts Black/Transparent, my code would work. All you’re doing right now is confirming if it’s a character, and if it is, you’re printing “else”. Is your goal to print “else” when a player touches it?
That was not my goal, my goal was to make models change colour to black and be transparent but I didn’t want the player but it was doing the player.
Wait… which model are you talking about?
The models in workspace but excluding the player.
So you’re saying ALL models in workspace?
Yes, that’s what I’m aiming for but it also could be a union or a part.
You have the completely wrong loop then. Try this instead:
for i,v in ipairs(workspace:GetChildren()) do
if v:IsA("Model") then
for i2,v2 in ipairs(v:GetDescendants()) do
if v:IsA("BasePart") then
--> change color here etc
end
end
end
end
This still didn’t work, I hope this is not getting to tricky:
cast1.Touched:Connect(function(hit)
for i,v in ipairs(workspace:GetChildren()) do
if v:IsA("Model") then
for i2,v2 in ipairs(v:GetDescendants()) do
if v:IsA("BasePart") then
v.BrickColor = BrickColor.Black()
end
end
end
end
end)
Errors? Are there any In the output?
Nope, even checked my errors.
I’ve found the root cause of the problem, it only touches the player. Is there any fixes though?