I’ve created a script that makes a player’s accessories transparent to 1 when they equip a weapon or tool, and sets the transparency back to 0 when the player unequips the item. While it works in Studio, it doesn’t seem to function properly on live servers. My objective is to change the transparency from the local/client side, if possible.
This is the code I used in my local script.:
tool.Equipped:connect(function()
for i=1, #c_children do
local instance = c_children[i]
if (instance.ClassName == "Accessory") then
if instance.Handle:FindFirstChild("BodyFrontAttachment") then --hides Front Accessories
instance.Handle.Transparency = 1
end
if instance.Handle:FindFirstChild("BodyBackAttachment") then --hides Back Accessories
instance.Handle.Transparency = 1
end
end
end
end)
tool.Unequipped:connect(function()
for i=1, #c_children do
local instance = c_children[i]
if (instance.ClassName == "Accessory") then
if instance.Handle:FindFirstChild("BodyFrontAttachment") then
instance.Handle.Transparency = 0
end
if instance.Handle:FindFirstChild("BodyBackAttachment") then
instance.Handle.Transparency = 0
end
end
end
end)
You shouldn’t use :connect anymore as it is deprecated due to outdated spelling, you should use :Connect instead.
I know this isn’t code review, but why so much repeated code?
You can make this into a for ... in loop:
for instance in c_children do
if (instance.ClassName == "Accessory") then
if instance.Handle:FindFirstChild("BodyFrontAttachment") then --hides Front Accessories
instance.Handle.Transparency = 1
end
if instance.Handle:FindFirstChild("BodyBackAttachment") then --hides Back Accessories
instance.Handle.Transparency = 1
end
end
end
Also, this may or may not be the cause of your issue but some accessories are subclasses to Accessory, so using :IsA might be wiser
if instance:IsA("Accessory") then
Why two statements doing the same thing? Just use or
if instance.Handle:FindFirstChild("BodyFrontAttachment") or instance.Handle:FindFirstChild("BodyBackAttachment") then --hides Back Accessories
instance.Handle.Transparency = 1
end
Finally, you could place them into the same function to not repeat yourself.
I.e.
function hideAccessories()
for instance: any in c_children do
if instance:IsA("Accessory") then
if instance.Handle:FindFirstChild("BodyFrontAttachment") or instance.Handle:FindFirstChild("BodyBackAttachment") then --hides Front Accessories
instance.Handle.Transparency = 1
end
end
end
end
tool.Equipped:Connect(hideAccessories)
tool.Unequipped:Connect(hideAccessories)
No, I wanted it to happen on the client-side because I believed that changing the transparency from the local/client-side would only affect it on that side. And that is my goal.
I grabbed a flashlight tool from the tool box, and added the code below, and its working in studio and a live game.
local tool = script.Parent
tool.Equipped:Connect(function()
local player = game.Players.LocalPlayer
if player and player.Character then
local character = player.Character
for _,i in pairs(character:GetChildren()) do
if i:IsA("Accessory") and i:FindFirstChild("Handle") then
if i.Handle:FindFirstChild("BodyFrontAttachment") then
i.Handle.Transparency = 1
end
if i.Handle:FindFirstChild("BodyBackAttachment") then
i.Handle.Transparency = 1
end
end
end
end
end)
tool.Unequipped:Connect(function()
local player = game.Players.LocalPlayer
if player and player.Character then
local character = player.Character
for _,i in pairs(character:GetChildren()) do
if i:IsA("Accessory") and i:FindFirstChild("Handle") then
if i.Handle:FindFirstChild("BodyFrontAttachment") then
i.Handle.Transparency = 0
end
if i.Handle:FindFirstChild("BodyBackAttachment") then
i.Handle.Transparency = 0
end
end
end
end
end)
Good news! I created a script by combining @SelDraken@majdTRM of your codes. Basically, I merged some of the code from each of your scripts to make it.
Here is the code (local side):
tool.Equipped:Connect(function()
for _,i in pairs(character:GetChildren()) do
if i:IsA("Accessory") and i:FindFirstChild("Handle") then
if i.Handle:FindFirstChild("BodyFrontAttachment") or i.Handle:FindFirstChild("BodyBackAttachment") then --hides Front Accessories
i.Handle.Transparency = 1
end
end
end
end)
tool.Unequipped:Connect(function()
for _,i in pairs(character:GetChildren()) do
if i:IsA("Accessory") and i:FindFirstChild("Handle") then
if i.Handle:FindFirstChild("BodyFrontAttachment") or i.Handle:FindFirstChild("BodyBackAttachment") then --hides Front Accessories
i.Handle.Transparency = 0
end
end
end
end)
Also no errors (ignore the other error line one that is a different topic)