Script working in ROBLOX Studio but not in live servers: Changing transparency from the client side

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)

If you use a client script it will only affect the client.
You need to have a server script do it.

From this I assumed it was OP’s intention for it to only affect the client-side, but maybe they worded their question wrong, I don’t know.

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.

That’s what I was thinking.

Anyway, can you try pressing F9 on your keyboard when in the game, and check the log section? Do you see any errors?

Is it an ‘acutal’ localscript or just a script that has its runcontext set to client?

ToolRemoveAccessories.rbxl (45.8 KB)

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)

I’m in studio but the “output” window has pretty much the same output the dev console log

and I found an error…

Accessories are never nested any deeper than the Character model, why not just iterate with Character:GetChildren()

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)

1 Like

Awesome! Glad I could help :slight_smile: ============

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.